版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xuan_xuan_2/article/details/82084729
1、 实例需求
从MainWindow窗体点击按钮打开MainWindow2窗体。
2、 实例实现
第一步:创建完项目之后,在点击 文件-新建文件或项目-文件和类-Qt-Qt 设计师界面类 如图1.1
单击选择弹出图1.2
单击下一步,弹出图1.3
单击下一步,弹出图1.3
单击完成,此时项目中又多了一个MainWindow2窗体。
第二步:打开MainWindow.h 头文件,编写代码。
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
//add by dx ---20180826
#include <QPushButton>
#include "mainwindow2.h"
//end by dx
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
//add by dx ---20180826
protected:
QPushButton *pButton;
MainWindow2 w2;
private slots:
void showMainWindow2();
//end by dx
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
打开MainWindow.cpp文件,编写代码。
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//add by dx ---20180826
//设置窗体标题
this->setWindowTitle("窗口一");
//定义按钮
pButton = new QPushButton(this);
pButton->setText("打开窗口2");
//关联
connect(pButton,SIGNAL(clicked(bool)),this,SLOT(showMainWindow2()));
//end by dx
}
MainWindow::~MainWindow()
{
delete ui;
}
//add by dx ---20180826
void MainWindow::showMainWindow2()
{
w2.show();
}
//end by dx
打开MainWindow2.cpp文件,编写代码:
#include "mainwindow2.h"
#include "ui_mainwindow2.h"
MainWindow2::MainWindow2(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow2)
{
ui->setupUi(this);
//add by dx ---20180826
//设置窗体标题
this->setWindowTitle("窗体二");
//end by dx
}
MainWindow2::~MainWindow2()
{
delete ui;
}
3、 效果图
今天讲解到现在结束了,想进行视频学习的小伙伴,可以进入我的视频教程进行学习,课程地址:https://edu.csdn.net/course/detail/7275,课程资源下载地址:https://download.csdn.net/download/xuan_xuan_2/10641568,欢迎大家前来学习交流。