使用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_Hmainwindow.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_Hdialog.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_Hmainwindow.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
- 使用Qt Designer和手动编写代码两种方式实现多窗口切换程序(Qt基础)
- Qt 不使用Qt Designer纯代码编写界面程序(初学者)总结
- Qt5基础(二)编写Qt多窗口程序笔记
- Qt多窗口切换程序
- Qt多窗口切换程序
- Struts2校验的两种实现方式(使用内置校验和手动编写)
- QT 编写Qt多窗口程序
- Qt 编写多窗口程序
- 编写Qt多窗口程序
- 编写Qt多窗口程序
- 编写QT多窗口程序
- QT实现多窗口切换
- QT实现多窗口切换
- QT(1)-Qt Creator 编写多窗口程序
- Qt基础(二)编写Qt多窗口程序遇到的问题
- Qt程序国际化工具使用和相关代码编写规范
- Linux下QT图形界面开发 用快速开发工具Qt Designer编写Qt程序(2)
- 二、Qt Creator编写多窗口程序
- IO-02. 整数四则运算(10)
- annotation-config, annotation-driven, compont-scan 区别
- 极域电子教室2014最新版
- 期末复习之宽度优先搜索
- Extjs 动态隐藏某个textfield以及fieldLabel组件的方法
- 使用Qt Designer和手动编写代码两种方式实现多窗口切换程序(Qt基础)
- IO-03. 求整数均值(10)
- JMF天昏地暗之路(二)-------打开摄像头并显示在屏幕上
- 顺序栈的实现
- Flash AS3 Study (只为记录一部分东西,到时候会转到新浪博客)
- 求3×3矩阵对角线元素之和
- maven学习教程(一)——环境搭建
- 微信公众平台开发(82) 天气预报
- KEIL下创建工程示例及配置简单说明