右键单击项目名,添加文件
选择widget
first.h
#ifndef FIRST_H
#define FIRST_H
#include <QWidget>
#include "second.h" //包含第二个窗口的头文件
namespace Ui {
class first;
}
class first : public QWidget
{
Q_OBJECT
public:
explicit first(QWidget *parent = 0);
~first();
signals:
void showWindowSignal(); //声明信号
private slots:
void showWindowSlot();
void gotoSecondWindow();
private:
Ui::first *ui;
second *secondWindow;
};
#endif // FIRST_H
first.cpp
#include "first.h"
#include "ui_first.h"
first::first(QWidget *parent) :
QWidget(parent),
ui(new Ui::first)
{
ui->setupUi(this);
secondWindow = new second(); //
secondWindow->hide(); //将第二个窗口隐藏
connect(ui->btn_goto,SIGNAL(clicked()), this, SLOT(gotoSecondWindow()));//将跳转按钮的点击事件绑定槽函数
connect(secondWindow, SIGNAL(showWindowSignal()), this, SLOT(showWindowSlot()));//将showWindosSignal信号和showWindowSlot槽函数关联
}
first::~first()
{
delete ui;
}
void first::showWindowSlot()//收到第二个窗口的信号时显示当前窗口
{
this->show();
}
void first::gotoSecondWindow()//跳转函数:隐藏当前窗口,显示第二个窗口
{
this->hide();
secondWindow->show();
}
second.h
#ifndef SECOND_H
#define SECOND_H
#include <QWidget>
namespace Ui {
class second;
}
class second : public QWidget
{
Q_OBJECT
public:
explicit second(QWidget *parent = 0);
~second();
signals:
void showWindowSignal();
private slots:
void gotoSecondWindow();
private:
Ui::second *ui;
};
#endif // SECOND_H
second.cpp
#include "second.h"
#include "ui_second.h"
second::second(QWidget *parent) :
QWidget(parent),
ui(new Ui::second)
{
ui->setupUi(this);
connect(ui->btn_goto, SIGNAL(clicked()), this, SLOT(gotoSecondWindow()));//将跳转按钮的点击事件绑定槽函数
}
second::~second()
{
delete ui;
}
void second::gotoSecondWindow()//跳转至第一个窗口,隐藏当前窗口,发送信号
{
this->hide();
emit showWindowSignal();
}
第二次弄的时候忘了类名首字母大写了,不太规范。