繁体   English   中英

带Qt4的C ++多窗口

[英]Multiple Window with C++ with Qt4

我正在尝试构建一个GUI应用程序,我正在通过Qt这样做。 我还想创建一个多窗口应用程序:我希望当我按下一个按钮时,另一个窗口显示(“隐藏”前一个窗口)。 那是GDI吗?

到目前为止,我已经为我想要的每个窗口(当前为4)创建了一个.ui文件,并且我正在尝试以这种方式连接它们(主窗口,另外3个)。

我怎么能这样做?

我正在发送该程序的文件,以使我的问题更加不可靠:

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()
{
}

mainwindow.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

您可以使用信号和插槽机制,为此请查看以下文档..

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

现在,您可以将一个窗口的信号连接到另一个窗口,并在另一个窗口的插槽中显示另一个窗口并隐藏前一个窗口。

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

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

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

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

一种方法是:

  • 将3个按钮Bi每一个关联到它们各自的窗口Wi (如地图,其中一个按键是按钮,值是QMainWindow)
  • 在窗口A创建一个自定义插槽,并将每个按钮的clicked()信号连接到此插槽。
  • 在此插槽中,您可以找到Bi使用sender()发送信号的按钮。 您找到关联的窗口Wi然后调用show() 同时你为所有其他窗口调用hide() Wj, j!=i

上面引用的所有方法都在QWidgetQobject的文档中,所以你应该阅读它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM