Flere vinduer med C ++ med Qt4

Indlæg af Hanne Mølgaard Plasc

Problem



Jeg forsøger at bygge en GUI app, og jeg gør dette gennem Qt. Jeg vil også oprette et multi-vindue program: Jeg vil have det, når jeg rammer en knap, vises det andet vindue ('gemmer' den forrige). Er det en GDI?


Hidtil har jeg oprettet en .ui-fil til hvert vindue, jeg vil have (i øjeblikket 4), og jeg forsøger at forbinde dem på den måde (hovedvinduet med de andre 3).


Hvordan kunne jeg gøre det?


Jeg sender filen af ​​programmet for at gøre mit problem mere ustatligt:


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", "Mat303251ria", 0, QApplication::UnicodeUTF8));

        const bool \_\_sortingEnabled = treeWidget->isSortingEnabled();
        treeWidget->setSortingEnabled(false);
        QTreeWidgetItem *\_\_\_qtreewidgetitem1 = treeWidget->topLevelItem(0);
        \_\_\_qtreewidgetitem1->setText(0, QApplication::translate("MainWindow", "Portugu303252s", 0, QApplication::UnicodeUTF8));
        QTreeWidgetItem *\_\_\_qtreewidgetitem2 = treeWidget->topLevelItem(1);
        \_\_\_qtreewidgetitem2->setText(0, QApplication::translate("MainWindow", "Reda303247303243o", 0, QApplication::UnicodeUTF8));
        QTreeWidgetItem *\_\_\_qtreewidgetitem3 = treeWidget->topLevelItem(2);
        \_\_\_qtreewidgetitem3->setText(0, QApplication::translate("MainWindow", "Matem303241tica", 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", "F303255sica", 0, QApplication::UnicodeUTF8));
        QTreeWidgetItem *\_\_\_qtreewidgetitem6 = treeWidget->topLevelItem(5);
        \_\_\_qtreewidgetitem6->setText(0, QApplication::translate("MainWindow", "Qu303255mica", 0, QApplication::UnicodeUTF8));
        QTreeWidgetItem *\_\_\_qtreewidgetitem7 = treeWidget->topLevelItem(6);
        \_\_\_qtreewidgetitem7->setText(0, QApplication::translate("MainWindow", "Hist303263ria", 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", "Ingl303252s", 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", "Calend303241rio 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", "Informa303247303265es", 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", "Voc303252 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

Bedste reference


Du kan bruge signal- og slotmekanismen, for det kan du gennemgå følgende dokumentation ..


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


Nu kan du forbinde signalet fra et vindue til et andet og i slot i det andet vindue viser du det andet vindue og skjuler det forrige.

Andre referencer 1


Nå, efter lidt tålmodighed og undersøgelse af Qt Architecture gjorde jeg det.
Jeg tilføjede lige den linje i Ui\_MainWindow::SetupUI(blablabla)


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


Og den respektive attribut til Ui\_MainWindow Class. Og nu arbejder det okay.

Andre referencer 2


Så du har et vindue A som 'styrer' de andre W1, W2, ...., Wn?


En måde er at gøre:



  • Tilknyt hver af de 3 knapper Bi til deres respektive vindue Wi (som et kort hvor en tast er en knap og værdien QMainWindow)

  • Opret en brugerdefineret plads i vinduet A og tilslut clicked() -signalerne til hver af knapperne til denne slot.

  • I denne slot finder du hvilken knap Bi sendte signalet ved hjælp af sender(). Du finder det tilhørende vindue Wi og du ringer til show(). I mellemtiden kalder du hide() for alle andre vinduer Wj, j!=i



Alle ovennævnte metoder er enten i dokumentet til QWidget eller Qobject, så du bør læse det.