使用Qt Designer和手动编写代码两种方式实现多窗口切换程序(Qt基础)

来源:互联网 发布:视频剪辑软件在线使用 编辑:程序博客网 时间:2024/05/14 18:53

今天花了一下午的时间学习了在Qt中如何进行多窗口切换的编程,网上看了很多教程,出了很多错误,找到了一个使用Qt Desinger编写的小程序,然后自己再试着用手动编写代码的方法实现多窗口切换,接下来分享一下我用这两种方法实现的多窗口编程,以及中间所出现的一些错误,供Qt Gui初学者参考。

一、使用手动编写代码

首先,我们的实现结果如下图所示:
                                  
从window2窗口点击按钮跳转到Dialog窗口,在点击Return按钮回到windows2窗口。实现代码分别如下:
dialog.h
#ifndef DIALOG_H#define DIALOG_H#include <QDialog>class QPushButton;class QLabel;class QVBoxLayout;class dialog : public QDialog{    Q_OBJECTpublic:    dialog(QWidget *parent = 0);    ~dialog();private:    QLabel *dialogLabel;  //显示文本    QPushButton *returnButton;  //返回按钮    QVBoxLayout *layout;  //竖向布局};#endif // DIALOG_H
mainwindow.h
#ifndef MAINWINDOW_H#define MAINWINDOW_H#include <QtGui/QMainWindow>#include "dialog.h"#include <QWidget>class QPushButton;class QLabel;class QVBoxLayout;class MainWindow : public QMainWindow{    Q_OBJECT    public:    MainWindow(QWidget *parent = 0);    ~MainWindow();private:    QLabel *mainLabel;    QPushButton *gotodialog1Button;    dialog dialog1;    QWidget *centerWindow;    QVBoxLayout *layout;private slots:    void gotodialog1();};#endif // MAINWINDOW_H
dialog.cpp
#include "dialog.h"#include <QPushButton>#include <QLabel>#include <QVBoxLayout>dialog::dialog(QWidget *parent):QDialog(parent){    dialogLabel = new QLabel(tr("Dialog"));    returnButton = new QPushButton("Return");    layout = new QVBoxLayout;    layout->addWidget(dialogLabel);    layout->addWidget(returnButton);    setLayout(layout);    setWindowTitle(tr("Dialog"));    connect(returnButton,SIGNAL(clicked()),this,SLOT(close()));}dialog::~dialog(){}
mainwindow.cpp
#include "mainwindow.h"#include <QPushButton>#include <dialog.h>#include <QVBoxLayout>#include <QLabel>MainWindow::MainWindow(QWidget *parent)    : QMainWindow(parent){    centerWindow = new QWidget;    setCentralWidget(centerWindow);    layout = new QVBoxLayout;    mainLabel = new QLabel(tr("Main Window"));    gotodialog1Button = new QPushButton("go to dialog1");    layout->addWidget(mainLabel);    layout->addWidget(gotodialog1Button);    centerWindow->setLayout(layout);    connect(gotodialog1Button,SIGNAL(clicked()),this,SLOT(gotodialog1()));}MainWindow::~MainWindow(){}void MainWindow::gotodialog1(){    this->hide();    dialog1.show();    dialog1.exec();    this->show();}
main.cpp
#include <QtGui/QApplication>#include "mainwindow.h"int main(int argc, char *argv[]){    QApplication a(argc, argv);    MainWindow w;    w.show();        return a.exec();}
首先我们在两个头文件dialog.h和mainwindow.h中定义了两个子类,分别用于创建两个窗口。在主窗口window2中,有一个按钮go to dialog1,这个按钮有Qt内置的信号clicked(),其槽自定义,这里定义为gotodialog1(),并且在构造函数中实现信号槽。

二、使用Qt Designer设计实现

首先,我们设计两个窗口,分别如下图:第二个是一个Dialog对话框。
   
分别继承这两个ui文件我们得到编写两个头文件分别如下:
dialog.h
#ifndef DIALOG_H#define DIALOG_H#include <QDialog>#include "Ui_dialog.h"class dialog:public QDialog,public Ui::Dialog{public:    dialog(QWidget *parent = 0);    ~dialog();};#endif // DIALOG_H
mainwindow.h
#ifndef MAINWINDOW_H#define MAINWINDOW_H#include <QMainWindow>#include "dialog.h"namespace Ui {class MainWindow;}class MainWindow : public QMainWindow{    Q_OBJECT    public:    explicit MainWindow(QWidget *parent = 0);    ~MainWindow();    private:    Ui::MainWindow *ui;    dialog dialog1;private slots:    void on_pushButton_clicked();};#endif // MAINWINDOW_H
然后是这两个子类的实现文件:
dialog.cpp
#include "dialog.h"dialog::dialog(QWidget *parent):QDialog(parent){    setupUi(this);}dialog::~dialog(){}
mainwindow.cpp
#include "mainwindow.h"#include "ui_mainwindow.h"MainWindow::MainWindow(QWidget *parent) :    QMainWindow(parent),    ui(new Ui::MainWindow){    ui->setupUi(this);}MainWindow::~MainWindow(){    delete ui;}void MainWindow::on_pushButton_clicked(){    this->hide();    dialog1.show();    dialog1.exec();    this->show();}
最后是主函数文件:
main.cpp
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    
    return a.exec();
}
运行结果如下:


三、一些错误总结

在QMainWindow对象中添加控件无法显示

下面的答案是在网上找到的:在QT Assistant中有提到:
Note: Creating a main window without a central widget is not supported. You must have a central widget even if it is just a placeholder.
即在QMainWindow对象中必须要有一个centraWidget,即必须要有一个模版,这里我们可以设置为widget,即在QMainWindow的子类中定义一个widget成员,将其作为centraWidget.


0 0