今天花了一下午的时间学习了在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_OBJECT
public:
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 *go