微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

具有Qt4的C的多窗口

我正在尝试构建一个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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐