Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) Qt QShowEvent example 1: visibility

 

QShowEvent example 1: visibility is a QShowEvent example that verifies that a widget is invisible in a dialog its constructor, but is visible in the showEvent member function.

 

 

 

 

 

 

Technical facts

 

Application type(s)

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

 

 

 

 

 

Qt project file: CppQShowEventExample1.pro

 

#-------------------------------------------------
#
# Project created by QtCreator 2013-05-04T15:13:50
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = CppQShowEventExample1
TEMPLATE = app


SOURCES += main.cpp\
        dialog.cpp

HEADERS  += dialog.h

FORMS    += dialog.ui

 

 

 

 

 

dialog.h

 

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include <QLabel>

namespace Ui {
  class Dialog;
}

class Dialog : public QDialog
{
  Q_OBJECT
  
public:
  explicit Dialog(QWidget *parent = 0);
  ~Dialog();

protected:
  void showEvent(QShowEvent *);
  
private:
  Ui::Dialog *ui;
  QLabel * const m_label;
};

#endif // DIALOG_H

 

 

 

 

 

dialog.cpp

 

#include "dialog.h"

#include <cassert>
#include <QDebug>
#include <QLabel>
#include <QShowEvent>
#include <QVBoxLayout>

#include "ui_dialog.h"

Dialog::Dialog(QWidget *parent) :
  QDialog(parent),
  ui(new Ui::Dialog),
  m_label(new QLabel("My label"))

{
  ui->setupUi(this);

  assert(!this->layout() && "No layout is present yet");
  this->setLayout(new QVBoxLayout);
  assert(this->layout() && "Vertical layout is present");

  this->layout()->addWidget(m_label);
  assert(!m_label->isVisible() && "Label is not visible yet");
  qDebug() << "End of constructor";
}

Dialog::~Dialog()
{
  delete ui;
}

void Dialog::showEvent(QShowEvent *)
{
  assert(m_label->isVisible() && "Label has been made visible now");
  qDebug() << "End of showEvent";
}

 

 

 

 

 

main.cpp

 

#include "dialog.h"
#include <QApplication>

int main(int argc, char *argv[])
{
  QApplication a(argc, argv);
  Dialog w;
  w.show();
  
  return a.exec();
}

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml