title: Multiple Window with C++ with Qt4
body: <p>I'm trying to build a GUI app, and I'm doing this through Qt. I also want to create a multi window application: I want that when I hit a button the other window shows up ("hiding" the previous one). Is that a GDI?</p>
  
  <p>So far, I have create a .ui file for every window I want (currently 4), and I'm trying to connect them that way (the main window, with the other 3).</p>
  
  <p>How could I do that?</p>
  
  <p>I'm sending the file of the program in order to make my problema more undestandable: </p>
  
  <p><strong>main.cpp</strong></p>
  
  <pre><code>#include &lt;QtGui/QApplication&gt;
  #include "mainwindow.h"
  
  int main(int argc, char *argv[])
  {
      QApplication a(argc, argv);
      MainWindow w;
      StudyWindow s;
  
      QStackedWidget *stackedWidget = new QStackedWidget;
      stackedWidget-&gt;addWidget(w);
      stackedWidget-&gt;addWidget(s);
  
      QVBoxLayout *layout = new QVBoxLayout;
      layout-&gt;addWidget(stackedWidget);
      setLayout(layout);
  
      w.show();
      return a.exec();
  }
  </code></pre>
  
  <p>mainwindow.cpp</p>
  
  <pre><code>#include "mainwindow.h"
  #include "ui_mainwindow.h"
  
  MainWindow::MainWindow(QWidget *parent) :
      QMainWindow(parent),
      ui(new Ui::MainWindow)
  {
      ui-&gt;setupUi(this);
      ConnectStuff();
  }
  
  MainWindow::~MainWindow()
  {
      delete ui;
  }
  
  void MainWindow::ConnectStuff()
  {
  }
  </code></pre>
  
  <p><strong>mainwindow.h</strong></p>
  
  <pre><code>#ifndef MAINWINDOW_H
  #define MAINWINDOW_H
  #include &lt;QMainWindow&gt;
  #include &lt;QPushButton&gt;
  #include &lt;QLayout&gt;
  #include &lt;QStackedWidget&gt;
  #include "study.h"
  namespace Ui {
      class MainWindow;
  }
  class MainWindow : public QMainWindow
  {
      Q_OBJECT
  public:
      void ConnectStuff();
      explicit MainWindow(QWidget *parent = 0);
      ~MainWindow();
  private:
      Ui::MainWindow *ui;
  };
  #endif // MAINWINDOW_H
  </code></pre>
  
  <p><strong>study.h</strong></p>
  
  <pre><code>#ifndef STUDYWINDOW_H
  #define STUDYWINDOW_H
  
  #include &lt;QMainWindow&gt;
  #include &lt;QPushButton&gt;
  
  namespace Ui {
      class StudyWindow;
  }
  
  class StudyWindow : public QMainWindow
  {
      Q_OBJECT
  
  public:
      explicit StudyWindow(QWidget *parent = 0);
      ~StudyWindow();
  
  private:
      Ui::StudyWindow *ui;
  };
  
  #endif // STUDYWINDOW_H
  </code></pre>
  
  <p><strong>ui_Study.h</strong></p>
  
  <pre><code>/********************************************************************************
  ** Form generated from reading UI file 'Study.ui'
  **
  ** Created: Tue 20. Mar 20:10:56 2012
  **      by: Qt User Interface Compiler version 4.7.4
  **
  ** WARNING! All changes made in this file will be lost when recompiling UI file!
  ********************************************************************************/
  
  #ifndef UI_STUDY_H
  #define UI_STUDY_H
  
  #include &lt;QtCore/QVariant&gt;
  #include &lt;QtGui/QAction&gt;
  #include &lt;QtGui/QApplication&gt;
  #include &lt;QtGui/QButtonGroup&gt;
  #include &lt;QtGui/QHeaderView&gt;
  #include &lt;QtGui/QMainWindow&gt;
  #include &lt;QtGui/QMenu&gt;
  #include &lt;QtGui/QMenuBar&gt;
  #include &lt;QtGui/QPushButton&gt;
  #include &lt;QtGui/QStatusBar&gt;
  #include &lt;QtGui/QTreeWidget&gt;
  #include &lt;QtGui/QWidget&gt;
  
  QT_BEGIN_NAMESPACE
  
  class Ui_StudyWindow
  {
  public:
      QAction *actionVoltar;
      QAction *actionSair;
      QWidget *centralwidget;
      QTreeWidget *treeWidget;
      QPushButton *pushButton;
      QMenuBar *menubar;
      QMenu *menuVoltar;
      QStatusBar *statusbar;
  
      void setupUi(QMainWindow *MainWindow)
      {
          if (MainWindow-&gt;objectName().isEmpty())
              MainWindow-&gt;setObjectName(QString::fromUtf8("MainWindow"));
          MainWindow-&gt;resize(800, 600);
          actionVoltar = new QAction(MainWindow);
          actionVoltar-&gt;setObjectName(QString::fromUtf8("actionVoltar"));
          actionSair = new QAction(MainWindow);
          actionSair-&gt;setObjectName(QString::fromUtf8("actionSair"));
          centralwidget = new QWidget(MainWindow);
          centralwidget-&gt;setObjectName(QString::fromUtf8("centralwidget"));
          treeWidget = new QTreeWidget(centralwidget);
          QFont font;
          font.setPointSize(8);
          font.setBold(true);
          font.setWeight(75);
          QTreeWidgetItem *__qtreewidgetitem = new QTreeWidgetItem();
          __qtreewidgetitem-&gt;setFont(0, font);
          treeWidget-&gt;setHeaderItem(__qtreewidgetitem);
          new QTreeWidgetItem(treeWidget);
          new QTreeWidgetItem(treeWidget);
          new QTreeWidgetItem(treeWidget);
          new QTreeWidgetItem(treeWidget);
          new QTreeWidgetItem(treeWidget);
          new QTreeWidgetItem(treeWidget);
          new QTreeWidgetItem(treeWidget);
          new QTreeWidgetItem(treeWidget);
          new QTreeWidgetItem(treeWidget);
          new QTreeWidgetItem(treeWidget);
          treeWidget-&gt;setObjectName(QString::fromUtf8("treeWidget"));
          treeWidget-&gt;setGeometry(QRect(0, 110, 161, 451));
          pushButton = new QPushButton(centralwidget);
          pushButton-&gt;setObjectName(QString::fromUtf8("pushButton"));
          pushButton-&gt;setGeometry(QRect(0, 0, 75, 23));
          MainWindow-&gt;setCentralWidget(centralwidget);
          menubar = new QMenuBar(MainWindow);
          menubar-&gt;setObjectName(QString::fromUtf8("menubar"));
          menubar-&gt;setGeometry(QRect(0, 0, 800, 21));
          menuVoltar = new QMenu(menubar);
          menuVoltar-&gt;setObjectName(QString::fromUtf8("menuVoltar"));
          MainWindow-&gt;setMenuBar(menubar);
          statusbar = new QStatusBar(MainWindow);
          statusbar-&gt;setObjectName(QString::fromUtf8("statusbar"));
          MainWindow-&gt;setStatusBar(statusbar);
  
          menubar-&gt;addAction(menuVoltar-&gt;menuAction());
          menuVoltar-&gt;addAction(actionVoltar);
          menuVoltar-&gt;addSeparator();
          menuVoltar-&gt;addAction(actionSair);
  
          retranslateUi(MainWindow);
  
          QMetaObject::connectSlotsByName(MainWindow);
      } // setupUi
  
      void retranslateUi(QMainWindow *MainWindow)
      {
          MainWindow-&gt;setWindowTitle(QApplication::translate("MainWindow", "MainWindow", 0, QApplication::UnicodeUTF8));
          actionVoltar-&gt;setText(QApplication::translate("MainWindow", "Voltar", 0, QApplication::UnicodeUTF8));
          actionSair-&gt;setText(QApplication::translate("MainWindow", "Sair", 0, QApplication::UnicodeUTF8));
          QTreeWidgetItem *___qtreewidgetitem = treeWidget-&gt;headerItem();
          ___qtreewidgetitem-&gt;setText(1, QApplication::translate("MainWindow", "Items", 0, QApplication::UnicodeUTF8));
          ___qtreewidgetitem-&gt;setText(0, QApplication::translate("MainWindow", "Mat\303\251ria", 0, QApplication::UnicodeUTF8));
  
          const bool __sortingEnabled = treeWidget-&gt;isSortingEnabled();
          treeWidget-&gt;setSortingEnabled(false);
          QTreeWidgetItem *___qtreewidgetitem1 = treeWidget-&gt;topLevelItem(0);
          ___qtreewidgetitem1-&gt;setText(0, QApplication::translate("MainWindow", "Portugu\303\252s", 0, QApplication::UnicodeUTF8));
          QTreeWidgetItem *___qtreewidgetitem2 = treeWidget-&gt;topLevelItem(1);
          ___qtreewidgetitem2-&gt;setText(0, QApplication::translate("MainWindow", "Reda\303\247\303\243o", 0, QApplication::UnicodeUTF8));
          QTreeWidgetItem *___qtreewidgetitem3 = treeWidget-&gt;topLevelItem(2);
          ___qtreewidgetitem3-&gt;setText(0, QApplication::translate("MainWindow", "Matem\303\241tica", 0, QApplication::UnicodeUTF8));
          QTreeWidgetItem *___qtreewidgetitem4 = treeWidget-&gt;topLevelItem(3);
          ___qtreewidgetitem4-&gt;setText(0, QApplication::translate("MainWindow", "Biologia", 0, QApplication::UnicodeUTF8));
          QTreeWidgetItem *___qtreewidgetitem5 = treeWidget-&gt;topLevelItem(4);
          ___qtreewidgetitem5-&gt;setText(0, QApplication::translate("MainWindow", "F\303\255sica", 0, QApplication::UnicodeUTF8));
          QTreeWidgetItem *___qtreewidgetitem6 = treeWidget-&gt;topLevelItem(5);
          ___qtreewidgetitem6-&gt;setText(0, QApplication::translate("MainWindow", "Qu\303\255mica", 0, QApplication::UnicodeUTF8));
          QTreeWidgetItem *___qtreewidgetitem7 = treeWidget-&gt;topLevelItem(6);
          ___qtreewidgetitem7-&gt;setText(0, QApplication::translate("MainWindow", "Hist\303\263ria", 0, QApplication::UnicodeUTF8));
          QTreeWidgetItem *___qtreewidgetitem8 = treeWidget-&gt;topLevelItem(7);
          ___qtreewidgetitem8-&gt;setText(0, QApplication::translate("MainWindow", "Geografia", 0, QApplication::UnicodeUTF8));
          QTreeWidgetItem *___qtreewidgetitem9 = treeWidget-&gt;topLevelItem(8);
          ___qtreewidgetitem9-&gt;setText(0, QApplication::translate("MainWindow", "Ingl\303\252s", 0, QApplication::UnicodeUTF8));
          QTreeWidgetItem *___qtreewidgetitem10 = treeWidget-&gt;topLevelItem(9);
          ___qtreewidgetitem10-&gt;setText(0, QApplication::translate("MainWindow", "Espanhol", 0, QApplication::UnicodeUTF8));
          treeWidget-&gt;setSortingEnabled(__sortingEnabled);
  
          pushButton-&gt;setText(QApplication::translate("MainWindow", "Cansei!", 0, QApplication::UnicodeUTF8));
          menuVoltar-&gt;setTitle(QApplication::translate("MainWindow", "Arquivo", 0, QApplication::UnicodeUTF8));
      } // retranslateUi
  
  };
  
  namespace Ui {
      class StudyWindow: public Ui_StudyWindow {};
  } // namespace Ui
  
  QT_END_NAMESPACE
  
  #endif // UI_STUDY_H
  </code></pre>
  
  <p><strong>ui_mainwindow.h</strong></p>
  
  <pre><code>/********************************************************************************
  ** Form generated from reading UI file 'mainwindow.ui'
  **
  ** Created: Tue 20. Mar 20:10:56 2012
  **      by: Qt User Interface Compiler version 4.7.4
  **
  ** WARNING! All changes made in this file will be lost when recompiling UI file!
  ********************************************************************************/
  
  #ifndef UI_MAINWINDOW_H
  #define UI_MAINWINDOW_H
  
  #include &lt;QtCore/QVariant&gt;
  #include &lt;QtGui/QAction&gt;
  #include &lt;QtGui/QApplication&gt;
  #include &lt;QtGui/QButtonGroup&gt;
  #include &lt;QtGui/QGroupBox&gt;
  #include &lt;QtGui/QHeaderView&gt;
  #include &lt;QtGui/QLabel&gt;
  #include &lt;QtGui/QMainWindow&gt;
  #include &lt;QtGui/QMenu&gt;
  #include &lt;QtGui/QMenuBar&gt;
  #include &lt;QtGui/QPlainTextEdit&gt;
  #include &lt;QtGui/QPushButton&gt;
  #include &lt;QtGui/QStatusBar&gt;
  #include &lt;QtGui/QWidget&gt;
  
  QT_BEGIN_NAMESPACE
  
  class Ui_MainWindow
  {
  public slots:
  public:
      QAction *actionAjuda;
      QAction *actionLista_de;
      QAction *actionSair;
      QWidget *centralwidget;
      QPushButton *pshBStudy;
      QPushButton *pshBSimulator;
      QPushButton *pshBExamCalen;
      QPushButton *pshBReadOfDay;
      QLabel *labelTitle;
      QPlainTextEdit *plainTextNews;
      QLabel *labelNews;
      QGroupBox *groupBox;
      QLabel *labelCollege;
      QLabel *labelCourse;
      QLabel *labelMemSince;
      QLabel *labelLoggedWith;
      QLabel *labelBP;
      QStatusBar *statusbar;
      QMenuBar *menuBar;
      QMenu *menuArquivo;
  
  
      void setupUi(QMainWindow *MainWindow)
      {
          if (MainWindow-&gt;objectName().isEmpty())
              MainWindow-&gt;setObjectName(QString::fromUtf8("MainWindow"));
          MainWindow-&gt;resize(800, 600);
          QSizePolicy sizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
          sizePolicy.setHorizontalStretch(0);
          sizePolicy.setVerticalStretch(0);
          sizePolicy.setHeightForWidth(MainWindow-&gt;sizePolicy().hasHeightForWidth());
          MainWindow-&gt;setSizePolicy(sizePolicy);
          actionAjuda = new QAction(MainWindow);
          actionAjuda-&gt;setObjectName(QString::fromUtf8("actionAjuda"));
          actionLista_de = new QAction(MainWindow);
          actionLista_de-&gt;setObjectName(QString::fromUtf8("actionLista_de"));
          actionSair = new QAction(MainWindow);
          actionSair-&gt;setObjectName(QString::fromUtf8("actionSair"));
          centralwidget = new QWidget(MainWindow);
          centralwidget-&gt;setObjectName(QString::fromUtf8("centralwidget"));
          pshBStudy = new QPushButton(centralwidget);
          pshBStudy-&gt;setObjectName(QString::fromUtf8("pshBStudy"));
          pshBStudy-&gt;setGeometry(QRect(140, 120, 161, 81));
          pshBStudy-&gt;setDefault(true);
          pshBSimulator = new QPushButton(centralwidget);
          pshBSimulator-&gt;setObjectName(QString::fromUtf8("pshBSimulator"));
          pshBSimulator-&gt;setGeometry(QRect(530, 120, 161, 81));
          pshBExamCalen = new QPushButton(centralwidget);
          pshBExamCalen-&gt;setObjectName(QString::fromUtf8("pshBExamCalen"));
          pshBExamCalen-&gt;setGeometry(QRect(140, 260, 161, 81));
          pshBReadOfDay = new QPushButton(centralwidget);
          pshBReadOfDay-&gt;setObjectName(QString::fromUtf8("pshBReadOfDay"));
          pshBReadOfDay-&gt;setGeometry(QRect(530, 260, 161, 81));
          labelTitle = new QLabel(centralwidget);
          labelTitle-&gt;setObjectName(QString::fromUtf8("labelTitle"));
          labelTitle-&gt;setGeometry(QRect(200, 10, 431, 71));
          QFont font;
          font.setPointSize(23);
          labelTitle-&gt;setFont(font);
          plainTextNews = new QPlainTextEdit(centralwidget);
          plainTextNews-&gt;setObjectName(QString::fromUtf8("plainTextNews"));
          plainTextNews-&gt;setGeometry(QRect(610, 440, 181, 111));
          plainTextNews-&gt;setReadOnly(true);
          labelNews = new QLabel(centralwidget);
          labelNews-&gt;setObjectName(QString::fromUtf8("labelNews"));
          labelNews-&gt;setGeometry(QRect(610, 420, 81, 16));
          groupBox = new QGroupBox(centralwidget);
          groupBox-&gt;setObjectName(QString::fromUtf8("groupBox"));
          groupBox-&gt;setGeometry(QRect(0, 460, 431, 91));
          labelCollege = new QLabel(groupBox);
          labelCollege-&gt;setObjectName(QString::fromUtf8("labelCollege"));
          labelCollege-&gt;setGeometry(QRect(230, 50, 111, 16));
          labelCourse = new QLabel(groupBox);
          labelCourse-&gt;setObjectName(QString::fromUtf8("labelCourse"));
          labelCourse-&gt;setGeometry(QRect(230, 30, 111, 16));
          labelMemSince = new QLabel(groupBox);
          labelMemSince-&gt;setObjectName(QString::fromUtf8("labelMemSince"));
          labelMemSince-&gt;setGeometry(QRect(10, 50, 111, 16));
          labelLoggedWith = new QLabel(groupBox);
          labelLoggedWith-&gt;setObjectName(QString::fromUtf8("labelLoggedWith"));
          labelLoggedWith-&gt;setGeometry(QRect(10, 30, 111, 16));
          labelBP = new QLabel(groupBox);
          labelBP-&gt;setObjectName(QString::fromUtf8("labelBP"));
          labelBP-&gt;setGeometry(QRect(10, 70, 111, 16));
          MainWindow-&gt;setCentralWidget(centralwidget);
          statusbar = new QStatusBar(MainWindow);
          statusbar-&gt;setObjectName(QString::fromUtf8("statusbar"));
          MainWindow-&gt;setStatusBar(statusbar);
          menuBar = new QMenuBar(MainWindow);
          menuBar-&gt;setObjectName(QString::fromUtf8("menuBar"));
          menuBar-&gt;setGeometry(QRect(0, 0, 800, 21));
          menuArquivo = new QMenu(menuBar);
          menuArquivo-&gt;setObjectName(QString::fromUtf8("menuArquivo"));
          MainWindow-&gt;setMenuBar(menuBar);
          QWidget::setTabOrder(pshBStudy, pshBSimulator);
          QWidget::setTabOrder(pshBSimulator, pshBExamCalen);
          QWidget::setTabOrder(pshBExamCalen, pshBReadOfDay);
          QWidget::setTabOrder(pshBReadOfDay, plainTextNews);
  
          menuBar-&gt;addAction(menuArquivo-&gt;menuAction());
          menuArquivo-&gt;addAction(actionSair);
  
  
          retranslateUi(MainWindow);
  
          QMetaObject::connectSlotsByName(MainWindow);
      } // setupUi
  
      void retranslateUi(QMainWindow *MainWindow)
      {
          MainWindow-&gt;setWindowTitle(QApplication::translate("MainWindow", "MainWindow", 0, QApplication::UnicodeUTF8));
          actionAjuda-&gt;setText(QApplication::translate("MainWindow", "Sobre...", 0, QApplication::UnicodeUTF8));
          actionLista_de-&gt;setText(QApplication::translate("MainWindow", "Lista de Provas", 0, QApplication::UnicodeUTF8));
          actionSair-&gt;setText(QApplication::translate("MainWindow", "Sair...", 0, QApplication::UnicodeUTF8));
          pshBStudy-&gt;setText(QApplication::translate("MainWindow", "Estudar!", 0, QApplication::UnicodeUTF8));
          pshBSimulator-&gt;setText(QApplication::translate("MainWindow", "Simulado", 0, QApplication::UnicodeUTF8));
          pshBExamCalen-&gt;setText(QApplication::translate("MainWindow", "Calend\303\241rio de Provas", 0, QApplication::UnicodeUTF8));
          pshBReadOfDay-&gt;setText(QApplication::translate("MainWindow", "Leitura do Dia", 0, QApplication::UnicodeUTF8));
          labelTitle-&gt;setText(QApplication::translate("MainWindow", "Escolha o que quer fazer hoje: ", 0, QApplication::UnicodeUTF8));
          labelNews-&gt;setText(QApplication::translate("MainWindow", "Novidades:", 0, QApplication::UnicodeUTF8));
          groupBox-&gt;setTitle(QApplication::translate("MainWindow", "Informa\303\247\303\265es", 0, QApplication::UnicodeUTF8));
          labelCollege-&gt;setText(QApplication::translate("MainWindow", "Faculdade:", 0, QApplication::UnicodeUTF8));
          labelCourse-&gt;setText(QApplication::translate("MainWindow", "Curso Pretendido:", 0, QApplication::UnicodeUTF8));
          labelMemSince-&gt;setText(QApplication::translate("MainWindow", "Membro desde:", 0, QApplication::UnicodeUTF8));
          labelLoggedWith-&gt;setText(QApplication::translate("MainWindow", "Voc\303\252 esta logado com: ", 0, QApplication::UnicodeUTF8));
          labelBP-&gt;setText(QApplication::translate("MainWindow", "BP: ", 0, QApplication::UnicodeUTF8));
          menuArquivo-&gt;setTitle(QApplication::translate("MainWindow", "Arquivo", 0, QApplication::UnicodeUTF8));
      } // retranslateUi
  
  };
  
  namespace Ui {
      class MainWindow: public Ui_MainWindow {};
  } // namespace Ui
  
  QT_END_NAMESPACE
  
  #endif // UI_MAINWINDOW_H
  </code></pre>