1、思路
a、分别有1 2 3号窗体
b、1号窗体,加载2号头文化并且实例化2号的空间
c、在1号窗体中,绑定2号窗体发送过来的信号,并用槽去处理(显示1号窗体)
d、在1号窗体按钮中show2号窗体
第二页面
返回第一个页面 到第二个页面
处理单击,到达第二个页面
2、新建2号窗体
3、效果及代码
oneform.cpp
#include "oneform.h"
#include "ui_oneform.h"
oneform::oneform(QWidget *parent) :
QWidget(parent),
ui(new Ui::oneform)
{
ui->setupUi(this);
Init();
}
oneform::~oneform()
{
delete ui;
}
void oneform::Init(){
//动画显示
QMovie *movie=new QMovie("../GIF/1.gif");
ui->label_1->setMovie(movie);
ui->label_1->setScaledContents(true);
movie->start();
// 处理点击第二页面的显示请求
connect(ui->btn_GoTo,SIGNAL(clicked()),this,SLOT(doProcessClickedGotoSecondForm()));
p_t2 =new secondform();//分配空间
//绑定,接收第二个页面的返回信号
connect(p_t2,SIGNAL(SignalshowOneForm()),this,SLOT(doProcessSecondFormRequest()));
}
void oneform::doProcessClickedGotoSecondForm()
{
qDebug()<<"clicked GoTo PushButton"<<endl;
// //第一种方式,创建第二个页面,出现多个2页面
// //secondform *tf1=new secondform(this);//子窗体会在父窗体直接显示
// secondform *tf1=new secondform(); //出现多个并行窗体,因为new 多个object
// tf1->show();
//第二种方式,跳转后只出现一个并列的窗体,因为其为全局变量
//t1.show();
//第三种方式, 指针全局变量,一个显示
p_t2->show();
this->hide();//第二个窗体显示后,关闭第一个窗体
}
void oneform::doProcessSecondFormRequest()
{
qDebug()<<"request"<<endl;
// qDebug()<<str<<endl;
this->show();
}
oneform.h
#ifndef ONEFORM_H
#define ONEFORM_H
#include <secondform.h>
#include <QWidget>
#include <QDebug>
#include <QMovie>
namespace Ui {
class oneform;
}
class oneform : public QWidget
{
Q_OBJECT
public:
explicit oneform(QWidget *parent = nullptr);
~oneform();
private:
Ui::oneform *ui;
secondform t1;//全局变量
secondform *p_t2;
private:
void Init();
private slots:
void doProcessClickedGotoSecondForm();
void doProcessSecondFormRequest();
};
#endif // ONEFORM_H
secondform.cpp
#include "secondform.h"
#include "ui_secondform.h"
#include <QDebug>
secondform::secondform(QWidget *parent) :
QWidget(parent),
ui(new Ui::secondform)
{
ui->setupUi(this);
Init();
}
secondform::~secondform()
{
delete ui;
}
void secondform::Init(){
ClickCount = 0;
//动画显示
QMovie *movie=new QMovie("../GIF/2.gif");
movie->start();
ui->label_1->setMovie(movie);
ui->label_1->setScaledContents(true);
//绑定点击事件
connect(ui->btn_back_one, SIGNAL(clicked(bool)),this,SLOT(doProcessClickedGotoOneForm()));
connect(ui->btn_Goto_third,SIGNAL(clicked(bool)),this,SLOT(doProcessClickedGotoThirdForm()));
p_third =new thirdform();
//绑定信号
connect(p_third,SIGNAL(SignalSecondFormShow()),this,SLOT(doProcessShow()));
}
//slots
void secondform::doProcessClickedGotoOneForm()
{
qDebug()<<"clicked back button"<<endl;
ClickCount++;
//发送信号,通知第一窗体显示
emit SignalshowOneForm();//点击次数转Qstring类型,发射返回信号
this->hide();//关闭当前窗口
}
void secondform::doProcessClickedGotoThirdForm()
{
p_third->show();
this->hide();
}
void secondform::doProcessShow(){
qDebug()<<"clicked GoTo Third button"<<endl;
this->show();
}
secondform.h
#ifndef SECONDFORM_H
#define SECONDFORM_H
#include <QWidget>
#include <QMovie>
#include "thirdform.h"
namespace Ui {
class secondform;
}
class secondform : public QWidget
{
Q_OBJECT
public:
explicit secondform(QWidget *parent = nullptr);
~secondform();
private:
Ui::secondform *ui;
int ClickCount;
void Init();
thirdform *p_third;
private slots:
void doProcessClickedGotoOneForm();
void doProcessClickedGotoThirdForm();
void doProcessShow();
signals:
void SignalshowOneForm();
};
#endif // SECONDFORM_H
thirdform.cpp
#include "thirdform.h"
#include "ui_thirdform.h"
#include <QDebug>
thirdform::thirdform(QWidget *parent) :
QWidget(parent),
ui(new Ui::thirdform)
{
ui->setupUi(this);
Init();
}
thirdform::~thirdform()
{
delete ui;
}
void thirdform::Init()
{
count = 0;
myTimer = new QTimer(this);
connect(myTimer,SIGNAL(timeout()),this,SLOT(doProcessTimeOut()));
myTimer->start(100);
//绑定返回第二个窗体信号
connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(doProcessBackSecondForm()));
connect(ui->btn_back,SIGNAL(clicked()),this,SLOT(doProcessBackSecondForm()));
}
//100ms超时处理函数
void thirdform::doProcessTimeOut(){
count++;;
if(count == 100){
count = 0;
}
ui->lcdNumber->display(count);
}
void thirdform::doProcessBackSecondForm()
{
//发送信息通知第二窗体,通知其显示
qDebug()<<"cliced back second form"<<endl;
emit SignalSecondFormShow();
this->hide();
}
thirdform.h
#ifndef THIRDFORM_H
#define THIRDFORM_H
#include <QWidget>
#include <QTimer>
namespace Ui {
class thirdform;
}
class thirdform : public QWidget
{
Q_OBJECT
public:
explicit thirdform(QWidget *parent = nullptr);
~thirdform();
private slots:
void doProcessTimeOut();
void doProcessBackSecondForm();
signals:
void SignalSecondFormShow();
private:
Ui::thirdform *ui;
int count;
QTimer *myTimer;
void Init();
};
#endif // THIRDFORM_H