(C++) Answer of exercise: Qt hide and show \#3

February 24, 2017 · View on GitHub

 

 

 

 

 

(C++) Answer of exercise: Qt hide and show #3

 

Answer of exercise: Qt hide and show #3 is the answer of the exercise Qt hide and show #3.

 

 

 

 

 

Solution

 

 

 

 

 

 

 

Qt project file: CppExerciseQtHideAndShow3Answer.pro

 


QT       += core gui TARGET = CppExerciseQtHideAndShow3Answer TEMPLATE = app SOURCES += \     main.cpp\     firstdialog.cpp \     seconddialog.cpp \     thirddialog.cpp \     qthideandshowdialog.cpp HEADERS  +=  \     firstdialog.h \     seconddialog.h \     thirddialog.h \     qthideandshowdialog.h FORMS    += \     firstdialog.ui \     seconddialog.ui \     thirddialog.ui

 

 

 

 

 

firstdialog.h

 


#ifndef FIRSTDIALOG_H #define FIRSTDIALOG_H #include "qthideandshowdialog.h" namespace Ui { class FirstDialog; } class FirstDialog : public QtHideAndShowDialog {   Q_OBJECT      public:   explicit FirstDialog(QWidget *parent = 0);   ~FirstDialog();      private slots:   void on_pushButton_clicked(); private:   Ui::FirstDialog *ui; }; #endif // FIRSTDIALOG_H

 

 

 

 

 

firstdialog.cpp

 


#include "seconddialog.h" #include "firstdialog.h" #include "ui_firstdialog.h" FirstDialog::FirstDialog(QWidget *parent) :     QtHideAndShowDialog(parent),     ui(new Ui::FirstDialog) {   ui->setupUi(this); } FirstDialog::~FirstDialog() {   delete ui; } void FirstDialog::on_pushButton_clicked() {   SecondDialog d;   this->ShowChild(&d); }

 

 

 

 

 

main.cpp

 


#include <QtGui/QApplication> #include "firstdialog.h" int main(int argc, char *argv[]) {   QApplication a(argc, argv);   FirstDialog w;   w.show();   a.exec(); }

 

 

 

 

 

qthideandshowdialog.h

 


#ifndef QTHIDEANDSHOWDIALOG_H #define QTHIDEANDSHOWDIALOG_H #include <QDialog> class QtHideAndShowDialog : public QDialog {     Q_OBJECT      public:   explicit QtHideAndShowDialog(QWidget *parent = 0);   ///Virtual destructor as this is a base class   virtual ~QtHideAndShowDialog() {}   ///Show a child   void ShowChild(QtHideAndShowDialog * const dialog); protected:   ///Shows a child until it emits a close_me signal   bool m_show_child;   ///closeEvent that emits the close_me signal   void closeEvent(QCloseEvent *); signals:   ///Emit the closeEvent of this dialog   void close_me(); protected slots:   ///Slot that needs to be called when a child signals close_me   void close_child(); }; #endif // QTHIDEANDSHOWDIALOG_H

 

 

 

 

 

qthideandshowdialog.cpp

 


#include <cassert> #include "qthideandshowdialog.h" QtHideAndShowDialog::QtHideAndShowDialog(QWidget *parent)   : QDialog(parent),     m_show_child(false) { } void QtHideAndShowDialog::close_child() {   m_show_child = false; } void QtHideAndShowDialog::closeEvent(QCloseEvent *) {   emit close_me(); } void QtHideAndShowDialog::ShowChild(QtHideAndShowDialog * const dialog) {   assert(dialog);   this->hide();   QObject::connect(dialog,SIGNAL(close_me()),this,SLOT(close_child()));   m_show_child = true;   while (m_show_child)   {     dialog->exec();   }   this->show(); }

 

 

 

 

 

seconddialog.h

 


#ifndef SECONDDIALOG_H #define SECONDDIALOG_H #include "qthideandshowdialog.h" namespace Ui { class SecondDialog; } class SecondDialog : public QtHideAndShowDialog {     Q_OBJECT      public:   explicit SecondDialog(QWidget *parent = 0);   ~SecondDialog();      private slots:   void on_button_goto_third_clicked();   void on_button_back_to_first_clicked(); private:   Ui::SecondDialog *ui; }; #endif // SECONDDIALOG_H

 

 

 

 

 

seconddialog.cpp

 


#include <cassert> #include "seconddialog.h" #include "ui_seconddialog.h" #include "thirddialog.h" SecondDialog::SecondDialog(QWidget *parent) :     QtHideAndShowDialog(parent),     ui(new Ui::SecondDialog) {   ui->setupUi(this); } SecondDialog::~SecondDialog() {   delete ui; } void SecondDialog::on_button_back_to_first_clicked() {   close(); } void SecondDialog::on_button_goto_third_clicked() {   ThirdDialog d;   this->ShowChild(&d);   if (d.m_back_to_which_dialog == 1)   {     this->close();   } }

 

 

 

 

 

thirddialog.h

 


#ifndef THIRDDIALOG_H #define THIRDDIALOG_H #include "qthideandshowdialog.h" namespace Ui { class ThirdDialog; } class ThirdDialog : public QtHideAndShowDialog {     Q_OBJECT      public:     explicit ThirdDialog(QWidget *parent = 0);     ~ThirdDialog();     int m_back_to_which_dialog; private slots:     void on_button_back_to_first_clicked();     void on_button_back_to_second_clicked(); private:     Ui::ThirdDialog *ui; }; #endif // THIRDDIALOG_H

 

 

 

 

 

thirddialog.cpp

 


#include "thirddialog.h" #include "ui_thirddialog.h" ThirdDialog::ThirdDialog(QWidget *parent) :     QtHideAndShowDialog(parent),     m_back_to_which_dialog(2), //When user closes the dialog, go back to the previous/second dialog     ui(new Ui::ThirdDialog) {   ui->setupUi(this); } ThirdDialog::~ThirdDialog() {   delete ui; } void ThirdDialog::on_button_back_to_first_clicked() {   m_back_to_which_dialog = 1;   close(); } void ThirdDialog::on_button_back_to_second_clicked() {   m_back_to_which_dialog = 2;   close(); }