1.新建一个Qt项目
选择创建的类型
2.文件->新建文件或项目-> Qt->Qt设计师类创建子界面
这里选择的是Main Window,命名为MainWindow2,按照同样方法创建MainWindow3
3.下面是代码部分
1.Mainwindow.h的代码:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include
#include
#include
#include
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
void dealmainwindow2(); //从子界面返回主界面函数
void dealmainwindow3();
private:
Ui::MainWindow *ui;
QPushButton * btnopen1; //定义关闭按钮
QPushButton * btnopen2; //声明新按钮
MainWindow2 w2; //声明新窗口
MainWindow3 w3; //声明新窗口
private slots:
void showMainwindow2(); //显示子页面槽函数
void showMainwindow3();
};
#endif // MAINWINDOW_H
2.MainWindow2.h代码:
#ifndef MAINWINDOW2_H
#define MAINWINDOW2_H
#include
#include
#include
#include
#include
#include
#include
#include
namespace Ui {
class MainWindow2;
}
class MainWindow2 : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow2(QWidget *parent = nullptr);
~MainWindow2();
void Sendslot(); //发送信号槽函数
void program(); //添加程序
void FileWrite(); //文本框输入
private:
Ui::MainWindow2 *ui;
signals:
void Mysignal(); //返回值信号
private:
QPushButton *returnButton; //控件声明
QPushButton *generateButton;
//QPushButton *OK;
//QTextEdit *wenjianjia; //文本框
//QTextEdit *shipinchangdu; //文本框
//QProcess* caller; //调用外部程序
private slots:
//void returnMainwindow(); //子页面
};
#endif // MAINWINDOW2_H
3.MainWindow3.h代码:
#ifndef MAINWINDOW3_H
#define MAINWINDOW3_H
#include
#include
#include
#include
#include
#include
#include
namespace Ui {
class MainWindow3;
}
class MainWindow3 : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow3(QWidget *parent = nullptr);
~MainWindow3();
void Sendslot(); //发送信号槽函数
//void program(); //添加程序
// void FileWrite1(); //将文本框内容写入txt文件中
//void FileWrite2();
private:
Ui::MainWindow3 *ui;
signals:
void Mysignal(); //返回值信号
private:
QPushButton *returnButton; //控件声明
QPushButton *generateButton;
//QPushButton *OK;
//QPushButton *Ok;
//QTextEdit *source;
//QTextEdit *direction;
};
#endif // MAINWINDOW3_H
4.Main.cpp代码不动
不添加东西
5.MainWindow.cpp代码:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QIcon"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->setWindowTitle("show");
this->setWindowFlags(Qt::WindowCloseButtonHint); //去掉最大化和最小化按钮,保留关闭按钮
btnopen1 = new QPushButton(this);
btnopen1->setText("子界面1");
btnopen1->move(350,300);
//链接信号与槽函数
//connect(btnopen1,SIGNAL(clicked()),this,SLOT(close()));
connect(btnopen1,SIGNAL(clicked()),this,SLOT(showMainwindow2()));
connect(&w3,&MainWindow3::Mysignal,this,&MainWindow::dealmainwindow2);
btnopen2 = new QPushButton(this);
btnopen2->setGeometry(QRect(350,350,100,25));
btnopen2->setText("子界面2");
connect(btnopen2,SIGNAL(clicked()),this,SLOT(showMainwindow3()));
connect(&w2,&MainWindow2::Mysignal,this,&MainWindow::dealmainwindow3);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::dealmainwindow2() //按钮回到主窗口
{
w2.hide();
this->show();
}
void MainWindow::dealmainwindow3() //按钮回到主窗口
{
w3.hide();
this->show();
}
void MainWindow::showMainwindow2() //按钮显示mainwindow2窗口,隐藏主窗口
{
this.hide();
w2.show();
}
void MainWindow::showMainwindow3() //按钮显示mainwindow3窗口
{
this.hide();
w3.show();
}
6.MainWindow2.cpp代码:
#include "mainwindow2.h"
#include "ui_mainwindow2.h"
MainWindow2::MainWindow2(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow2)
{
ui->setupUi(this);
this->setWindowTitle("子界面1");
returnButton = new QPushButton(this);
returnButton->setText("返回主窗口");
returnButton->move(350,350);
generateButton=new QPushButton(this);
generateButton->setText("功能按键"); //子界面实现其功能的按键
generateButton->move(350,300);
connect(returnButton,&QPushButton::clicked,this,&MainWindow2::Sendslot);
connect(generateButton,&QPushButton::clicked,this,&MainWindow2::program);
}
MainWindow2::~MainWindow2()
{
delete ui;
}
void MainWindow2::Sendslot()
{
emit Mysignal();
}
void MainWindow2::program() //打开外部程序
{
this->hide();
//添加子界面2的功能代码,可以打开外部程序
}
7.MainWindow3.cpp代码:
#include "mainwindow3.h"
#include "ui_mainwindow3.h"
MainWindow3::MainWindow3(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow3)
{
ui->setupUi(this);
ui->setupUi(this);
this->setWindowTitle("子界面2");
returnButton = new QPushButton(this);
returnButton->setText("返回主窗口");
returnButton->move(350,350);
generateButton=new QPushButton(this);
generateButton->setText("功能按键");
generateButton->move(350,300);
connect(returnButton,&QPushButton::clicked,this,&MainWindow3::Sendslot);
connect(generateButton,&QPushButton::clicked,this,&MainWindow3::program);
}
MainWindow3::~MainWindow3()
{
delete ui;
}
void MainWindow3::Sendslot()
{
emit Mysignal();
}
void MainWindow3::program() //实现功能的代码放置在这里
{
this->hide();
}