2012-03-21 63 views
3

我试图建立一个GUI应用程序,我通过Qt的事情了。我也想创建一个多窗口应用程序:我希望当我点击一个按钮时,另一个窗口显示出来(“隐藏”前一个窗口)。这是一个GDI吗?多窗口与C++与Qt4的

到目前为止,我已创建的每一个窗口我想(目前4)的.ui文件,我试图连接他们的方式(主要窗口,与其他3)。

我该怎么做?

我发送程序的文件,以使我的problema更undestandable:

的main.cpp

#include <QtGui/QApplication> 
#include "mainwindow.h" 

int main(int argc, char *argv[]) 
{ 
    QApplication a(argc, argv); 
    MainWindow w; 
    StudyWindow s; 

    QStackedWidget *stackedWidget = new QStackedWidget; 
    stackedWidget->addWidget(w); 
    stackedWidget->addWidget(s); 

    QVBoxLayout *layout = new QVBoxLayout; 
    layout->addWidget(stackedWidget); 
    setLayout(layout); 

    w.show(); 
    return a.exec(); 
} 

mainwindow.cpp

#include "mainwindow.h" 
#include "ui_mainwindow.h" 

MainWindow::MainWindow(QWidget *parent) : 
    QMainWindow(parent), 
    ui(new Ui::MainWindow) 
{ 
    ui->setupUi(this); 
    ConnectStuff(); 
} 

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

void MainWindow::ConnectStuff() 
{ 
} 

主窗口.H

#ifndef MAINWINDOW_H 
#define MAINWINDOW_H 
#include <QMainWindow> 
#include <QPushButton> 
#include <QLayout> 
#include <QStackedWidget> 
#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 

study.h

#ifndef STUDYWINDOW_H 
#define STUDYWINDOW_H 

#include <QMainWindow> 
#include <QPushButton> 

namespace Ui { 
    class StudyWindow; 
} 

class StudyWindow : public QMainWindow 
{ 
    Q_OBJECT 

public: 
    explicit StudyWindow(QWidget *parent = 0); 
    ~StudyWindow(); 

private: 
    Ui::StudyWindow *ui; 
}; 

#endif // STUDYWINDOW_H 

ui_Study.h

/******************************************************************************** 
** 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 <QtCore/QVariant> 
#include <QtGui/QAction> 
#include <QtGui/QApplication> 
#include <QtGui/QButtonGroup> 
#include <QtGui/QHeaderView> 
#include <QtGui/QMainWindow> 
#include <QtGui/QMenu> 
#include <QtGui/QMenuBar> 
#include <QtGui/QPushButton> 
#include <QtGui/QStatusBar> 
#include <QtGui/QTreeWidget> 
#include <QtGui/QWidget> 

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->objectName().isEmpty()) 
      MainWindow->setObjectName(QString::fromUtf8("MainWindow")); 
     MainWindow->resize(800, 600); 
     actionVoltar = new QAction(MainWindow); 
     actionVoltar->setObjectName(QString::fromUtf8("actionVoltar")); 
     actionSair = new QAction(MainWindow); 
     actionSair->setObjectName(QString::fromUtf8("actionSair")); 
     centralwidget = new QWidget(MainWindow); 
     centralwidget->setObjectName(QString::fromUtf8("centralwidget")); 
     treeWidget = new QTreeWidget(centralwidget); 
     QFont font; 
     font.setPointSize(8); 
     font.setBold(true); 
     font.setWeight(75); 
     QTreeWidgetItem *__qtreewidgetitem = new QTreeWidgetItem(); 
     __qtreewidgetitem->setFont(0, font); 
     treeWidget->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->setObjectName(QString::fromUtf8("treeWidget")); 
     treeWidget->setGeometry(QRect(0, 110, 161, 451)); 
     pushButton = new QPushButton(centralwidget); 
     pushButton->setObjectName(QString::fromUtf8("pushButton")); 
     pushButton->setGeometry(QRect(0, 0, 75, 23)); 
     MainWindow->setCentralWidget(centralwidget); 
     menubar = new QMenuBar(MainWindow); 
     menubar->setObjectName(QString::fromUtf8("menubar")); 
     menubar->setGeometry(QRect(0, 0, 800, 21)); 
     menuVoltar = new QMenu(menubar); 
     menuVoltar->setObjectName(QString::fromUtf8("menuVoltar")); 
     MainWindow->setMenuBar(menubar); 
     statusbar = new QStatusBar(MainWindow); 
     statusbar->setObjectName(QString::fromUtf8("statusbar")); 
     MainWindow->setStatusBar(statusbar); 

     menubar->addAction(menuVoltar->menuAction()); 
     menuVoltar->addAction(actionVoltar); 
     menuVoltar->addSeparator(); 
     menuVoltar->addAction(actionSair); 

     retranslateUi(MainWindow); 

     QMetaObject::connectSlotsByName(MainWindow); 
    } // setupUi 

    void retranslateUi(QMainWindow *MainWindow) 
    { 
     MainWindow->setWindowTitle(QApplication::translate("MainWindow", "MainWindow", 0, QApplication::UnicodeUTF8)); 
     actionVoltar->setText(QApplication::translate("MainWindow", "Voltar", 0, QApplication::UnicodeUTF8)); 
     actionSair->setText(QApplication::translate("MainWindow", "Sair", 0, QApplication::UnicodeUTF8)); 
     QTreeWidgetItem *___qtreewidgetitem = treeWidget->headerItem(); 
     ___qtreewidgetitem->setText(1, QApplication::translate("MainWindow", "Items", 0, QApplication::UnicodeUTF8)); 
     ___qtreewidgetitem->setText(0, QApplication::translate("MainWindow", "Mat\303\251ria", 0, QApplication::UnicodeUTF8)); 

     const bool __sortingEnabled = treeWidget->isSortingEnabled(); 
     treeWidget->setSortingEnabled(false); 
     QTreeWidgetItem *___qtreewidgetitem1 = treeWidget->topLevelItem(0); 
     ___qtreewidgetitem1->setText(0, QApplication::translate("MainWindow", "Portugu\303\252s", 0, QApplication::UnicodeUTF8)); 
     QTreeWidgetItem *___qtreewidgetitem2 = treeWidget->topLevelItem(1); 
     ___qtreewidgetitem2->setText(0, QApplication::translate("MainWindow", "Reda\303\247\303\243o", 0, QApplication::UnicodeUTF8)); 
     QTreeWidgetItem *___qtreewidgetitem3 = treeWidget->topLevelItem(2); 
     ___qtreewidgetitem3->setText(0, QApplication::translate("MainWindow", "Matem\303\241tica", 0, QApplication::UnicodeUTF8)); 
     QTreeWidgetItem *___qtreewidgetitem4 = treeWidget->topLevelItem(3); 
     ___qtreewidgetitem4->setText(0, QApplication::translate("MainWindow", "Biologia", 0, QApplication::UnicodeUTF8)); 
     QTreeWidgetItem *___qtreewidgetitem5 = treeWidget->topLevelItem(4); 
     ___qtreewidgetitem5->setText(0, QApplication::translate("MainWindow", "F\303\255sica", 0, QApplication::UnicodeUTF8)); 
     QTreeWidgetItem *___qtreewidgetitem6 = treeWidget->topLevelItem(5); 
     ___qtreewidgetitem6->setText(0, QApplication::translate("MainWindow", "Qu\303\255mica", 0, QApplication::UnicodeUTF8)); 
     QTreeWidgetItem *___qtreewidgetitem7 = treeWidget->topLevelItem(6); 
     ___qtreewidgetitem7->setText(0, QApplication::translate("MainWindow", "Hist\303\263ria", 0, QApplication::UnicodeUTF8)); 
     QTreeWidgetItem *___qtreewidgetitem8 = treeWidget->topLevelItem(7); 
     ___qtreewidgetitem8->setText(0, QApplication::translate("MainWindow", "Geografia", 0, QApplication::UnicodeUTF8)); 
     QTreeWidgetItem *___qtreewidgetitem9 = treeWidget->topLevelItem(8); 
     ___qtreewidgetitem9->setText(0, QApplication::translate("MainWindow", "Ingl\303\252s", 0, QApplication::UnicodeUTF8)); 
     QTreeWidgetItem *___qtreewidgetitem10 = treeWidget->topLevelItem(9); 
     ___qtreewidgetitem10->setText(0, QApplication::translate("MainWindow", "Espanhol", 0, QApplication::UnicodeUTF8)); 
     treeWidget->setSortingEnabled(__sortingEnabled); 

     pushButton->setText(QApplication::translate("MainWindow", "Cansei!", 0, QApplication::UnicodeUTF8)); 
     menuVoltar->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 

ui_mainwindow.h

/******************************************************************************** 
** 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 <QtCore/QVariant> 
#include <QtGui/QAction> 
#include <QtGui/QApplication> 
#include <QtGui/QButtonGroup> 
#include <QtGui/QGroupBox> 
#include <QtGui/QHeaderView> 
#include <QtGui/QLabel> 
#include <QtGui/QMainWindow> 
#include <QtGui/QMenu> 
#include <QtGui/QMenuBar> 
#include <QtGui/QPlainTextEdit> 
#include <QtGui/QPushButton> 
#include <QtGui/QStatusBar> 
#include <QtGui/QWidget> 

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->objectName().isEmpty()) 
      MainWindow->setObjectName(QString::fromUtf8("MainWindow")); 
     MainWindow->resize(800, 600); 
     QSizePolicy sizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred); 
     sizePolicy.setHorizontalStretch(0); 
     sizePolicy.setVerticalStretch(0); 
     sizePolicy.setHeightForWidth(MainWindow->sizePolicy().hasHeightForWidth()); 
     MainWindow->setSizePolicy(sizePolicy); 
     actionAjuda = new QAction(MainWindow); 
     actionAjuda->setObjectName(QString::fromUtf8("actionAjuda")); 
     actionLista_de = new QAction(MainWindow); 
     actionLista_de->setObjectName(QString::fromUtf8("actionLista_de")); 
     actionSair = new QAction(MainWindow); 
     actionSair->setObjectName(QString::fromUtf8("actionSair")); 
     centralwidget = new QWidget(MainWindow); 
     centralwidget->setObjectName(QString::fromUtf8("centralwidget")); 
     pshBStudy = new QPushButton(centralwidget); 
     pshBStudy->setObjectName(QString::fromUtf8("pshBStudy")); 
     pshBStudy->setGeometry(QRect(140, 120, 161, 81)); 
     pshBStudy->setDefault(true); 
     pshBSimulator = new QPushButton(centralwidget); 
     pshBSimulator->setObjectName(QString::fromUtf8("pshBSimulator")); 
     pshBSimulator->setGeometry(QRect(530, 120, 161, 81)); 
     pshBExamCalen = new QPushButton(centralwidget); 
     pshBExamCalen->setObjectName(QString::fromUtf8("pshBExamCalen")); 
     pshBExamCalen->setGeometry(QRect(140, 260, 161, 81)); 
     pshBReadOfDay = new QPushButton(centralwidget); 
     pshBReadOfDay->setObjectName(QString::fromUtf8("pshBReadOfDay")); 
     pshBReadOfDay->setGeometry(QRect(530, 260, 161, 81)); 
     labelTitle = new QLabel(centralwidget); 
     labelTitle->setObjectName(QString::fromUtf8("labelTitle")); 
     labelTitle->setGeometry(QRect(200, 10, 431, 71)); 
     QFont font; 
     font.setPointSize(23); 
     labelTitle->setFont(font); 
     plainTextNews = new QPlainTextEdit(centralwidget); 
     plainTextNews->setObjectName(QString::fromUtf8("plainTextNews")); 
     plainTextNews->setGeometry(QRect(610, 440, 181, 111)); 
     plainTextNews->setReadOnly(true); 
     labelNews = new QLabel(centralwidget); 
     labelNews->setObjectName(QString::fromUtf8("labelNews")); 
     labelNews->setGeometry(QRect(610, 420, 81, 16)); 
     groupBox = new QGroupBox(centralwidget); 
     groupBox->setObjectName(QString::fromUtf8("groupBox")); 
     groupBox->setGeometry(QRect(0, 460, 431, 91)); 
     labelCollege = new QLabel(groupBox); 
     labelCollege->setObjectName(QString::fromUtf8("labelCollege")); 
     labelCollege->setGeometry(QRect(230, 50, 111, 16)); 
     labelCourse = new QLabel(groupBox); 
     labelCourse->setObjectName(QString::fromUtf8("labelCourse")); 
     labelCourse->setGeometry(QRect(230, 30, 111, 16)); 
     labelMemSince = new QLabel(groupBox); 
     labelMemSince->setObjectName(QString::fromUtf8("labelMemSince")); 
     labelMemSince->setGeometry(QRect(10, 50, 111, 16)); 
     labelLoggedWith = new QLabel(groupBox); 
     labelLoggedWith->setObjectName(QString::fromUtf8("labelLoggedWith")); 
     labelLoggedWith->setGeometry(QRect(10, 30, 111, 16)); 
     labelBP = new QLabel(groupBox); 
     labelBP->setObjectName(QString::fromUtf8("labelBP")); 
     labelBP->setGeometry(QRect(10, 70, 111, 16)); 
     MainWindow->setCentralWidget(centralwidget); 
     statusbar = new QStatusBar(MainWindow); 
     statusbar->setObjectName(QString::fromUtf8("statusbar")); 
     MainWindow->setStatusBar(statusbar); 
     menuBar = new QMenuBar(MainWindow); 
     menuBar->setObjectName(QString::fromUtf8("menuBar")); 
     menuBar->setGeometry(QRect(0, 0, 800, 21)); 
     menuArquivo = new QMenu(menuBar); 
     menuArquivo->setObjectName(QString::fromUtf8("menuArquivo")); 
     MainWindow->setMenuBar(menuBar); 
     QWidget::setTabOrder(pshBStudy, pshBSimulator); 
     QWidget::setTabOrder(pshBSimulator, pshBExamCalen); 
     QWidget::setTabOrder(pshBExamCalen, pshBReadOfDay); 
     QWidget::setTabOrder(pshBReadOfDay, plainTextNews); 

     menuBar->addAction(menuArquivo->menuAction()); 
     menuArquivo->addAction(actionSair); 


     retranslateUi(MainWindow); 

     QMetaObject::connectSlotsByName(MainWindow); 
    } // setupUi 

    void retranslateUi(QMainWindow *MainWindow) 
    { 
     MainWindow->setWindowTitle(QApplication::translate("MainWindow", "MainWindow", 0, QApplication::UnicodeUTF8)); 
     actionAjuda->setText(QApplication::translate("MainWindow", "Sobre...", 0, QApplication::UnicodeUTF8)); 
     actionLista_de->setText(QApplication::translate("MainWindow", "Lista de Provas", 0, QApplication::UnicodeUTF8)); 
     actionSair->setText(QApplication::translate("MainWindow", "Sair...", 0, QApplication::UnicodeUTF8)); 
     pshBStudy->setText(QApplication::translate("MainWindow", "Estudar!", 0, QApplication::UnicodeUTF8)); 
     pshBSimulator->setText(QApplication::translate("MainWindow", "Simulado", 0, QApplication::UnicodeUTF8)); 
     pshBExamCalen->setText(QApplication::translate("MainWindow", "Calend\303\241rio de Provas", 0, QApplication::UnicodeUTF8)); 
     pshBReadOfDay->setText(QApplication::translate("MainWindow", "Leitura do Dia", 0, QApplication::UnicodeUTF8)); 
     labelTitle->setText(QApplication::translate("MainWindow", "Escolha o que quer fazer hoje: ", 0, QApplication::UnicodeUTF8)); 
     labelNews->setText(QApplication::translate("MainWindow", "Novidades:", 0, QApplication::UnicodeUTF8)); 
     groupBox->setTitle(QApplication::translate("MainWindow", "Informa\303\247\303\265es", 0, QApplication::UnicodeUTF8)); 
     labelCollege->setText(QApplication::translate("MainWindow", "Faculdade:", 0, QApplication::UnicodeUTF8)); 
     labelCourse->setText(QApplication::translate("MainWindow", "Curso Pretendido:", 0, QApplication::UnicodeUTF8)); 
     labelMemSince->setText(QApplication::translate("MainWindow", "Membro desde:", 0, QApplication::UnicodeUTF8)); 
     labelLoggedWith->setText(QApplication::translate("MainWindow", "Voc\303\252 esta logado com: ", 0, QApplication::UnicodeUTF8)); 
     labelBP->setText(QApplication::translate("MainWindow", "BP: ", 0, QApplication::UnicodeUTF8)); 
     menuArquivo->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 
+3

你有任何意思吗[MDI](http://en.wikipedia.org/wiki/Multiple_document_interface)? [这是Qt自带的一个很好的例子](http://qt-project.org/doc/qt-4.8/mainwindows-mdi.html)。 – Bart 2012-03-21 09:15:13

+1

找你我需要[QStackedWidget](http://qt-project.org/doc/qt-4.8/qstackedwidget.html) – 2012-03-21 09:54:37

+0

@Bart是的,我的意思是说。我与GDD犯了一个错误。 – 2012-03-21 16:24:39

回答

2

好吧,经过一些耐心和对Qt架构的研究,我做到了。 我刚添加线在Ui_MainWindow::SetupUI(blablabla)

studyWindow = new StudyWindow(MainWindow); 
QObject::connect(pshBStudy, SIGNAL(clicked()), studyWindow, SLOT(show())); 

和相应的属性来Ui_MainWindow类。现在它工作正常。

3

您可以使用信号和插槽机制,对于请到通过下列文件..

http://qt-project.org/doc/qt-4.8/signalsandslots.html

现在,您可以连接一个窗口到另一个的信号,并在其他窗口的缝隙你显示另一个窗口并隐藏前一个窗口。

+0

事情是,窗户是在不同的班级,哪一个不能访问另一个班级。我会发布代码,以便检查出来。 – 2012-03-21 16:33:58

+0

你只需要实例化该类的对象,并调用该窗口的show方法,并隐藏另一个... – shofee 2012-03-21 18:13:49

1

所以,你有一个窗口A其中“控制”其他W1, W2, ...., Wn

的一种方法是要做到:各3个按钮Bi

  • 准到它们各自的窗口Wi(如地图,其中一个关键是一个按钮,该值的​​QMainWindow)
  • 创建在窗口A定制槽和每个按钮的clicked()信号连接到该插槽。
  • 在这个插槽中你会发现使用sender()哪个按钮Bi发送的信号。您可以找到关联的窗口Wi,并且您拨打show()。在此期间,你叫hide()所有其他窗口Wj, j!=i

上面提到的所有方法或者在QWidgetQobject的文档,所以你应该看它。