创建新界面类的方法:
右击工程-->add new-->
-->选择相应的窗体类
方法一:信号槽函数法
oneform.h
#ifndef ONEFORM_H
#define ONEFORM_H
#include <QWidget>
#include "twoform.h" //要创建第二界面,所以需要第二界面的头文件
QT_BEGIN_NAMESPACE
namespace Ui { class oneform; }
QT_END_NAMESPACE
class oneform : public QWidget
{
Q_OBJECT
public:
oneform(QWidget *parent = nullptr);
~oneform();
public slots:
void one_two(void); //第一界面进入第二界面的槽函数
void oneshow(void); //显示界面1,隐藏界面2
private:
Ui::oneform *ui;
twoform* win; //创建第二界面
};
#endif // ONEFORM_H
oneform.cpp
#include "oneform.h"
#include "ui_oneform.h"
oneform::oneform(QWidget *parent)
: QWidget(parent)
, ui(new Ui::oneform)
{
ui->setupUi(this);
connect(ui->pushButton,SIGNAL(clicked(void)),this,SLOT(one_two(void)));
win=new twoform; //创建界面2
connect(win,SIGNAL(oneFunc(void)),this,SLOT(oneshow(void)));
//接受界面2发送的信号
}
oneform::~oneform()
{
delete ui;
}
void oneform::one_two()
{
win->show();
this->hide();
}
void oneform::oneshow()
{
this->show();
}
twoform.h
#ifndef TWOFORM_H
#define TWOFORM_H
#include <QWidget>
namespace Ui {
class twoform;
}
class twoform : public QWidget
{
Q_OBJECT
public:
explicit twoform(QWidget *parent = nullptr);
~twoform();
private:
Ui::twoform *ui;
public slots:
void two_one(void); //第二界面进入第一界面的槽函数
signals:
void oneFunc(void);
};
#endif // TWOFORM_H
twoform.cpp
#include "twoform.h"
#include "ui_twoform.h"
twoform::twoform(QWidget *parent) :
QWidget(parent),
ui(new Ui::twoform)
{
ui->setupUi(this);
connect(ui->pushButton,SIGNAL(clicked(void)),this,SLOT(two_one(void)));
}
twoform::~twoform()
{
delete ui;
}
void twoform::two_one()
{
emit oneFunc(); //发射信号,通知界面1显示---界面1需要接受这个信号
this->hide();
}
工程和视频教程下载:https://pan.baidu.com/s/1jgycJT4W_7QZLMlFvfo7zQ
提取码:6666
方法二:静态成员法
工程和视频教程下载:https://pan.baidu.com/s/1V65MsG97amOVPt45ifnjIg
提取码:6666