C++,QT多个窗体切换,widget,QTimer 3个页面切换跳转,使用wdiget 的方式
先看效果:
onewidget.h
#ifndef ONEWIDGET_H
#define ONEWIDGET_H
#include <QWidget>
#include "twowidget.h"
#include "threewidget.h"
QT_BEGIN_NAMESPACE
namespace Ui { class OneWidget; }
QT_END_NAMESPACE
class OneWidget : public QWidget
{
Q_OBJECT
public:
OneWidget(QWidget *parent = nullptr);
~OneWidget();
private slots:
void vBtnJump3Process(void);
void vBtnJump2Process(void);
void vBtnJump1Process(int);
private:
Ui::OneWidget *ui;
twowidget *pPage2;
threeWidget *pPage3;
void vInit(void);
};
#endif // ONEWIDGET_H
onewidget.cpp
#include "onewidget.h"
#include "ui_onewidget.h"
#include <QMovie>
#include <QDebug>
OneWidget::OneWidget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::OneWidget)
{
ui->setupUi(this);
this->vInit();
}
OneWidget::~OneWidget()
{
delete ui;
}
void OneWidget::vInit(void)
{
this->move(0,0);
this->setWindowFlag(Qt::FramelessWindowHint);
QMovie *pMovie=new QMovie("./dan.gif");
ui->label->setMovie(pMovie);
ui->label->setScaledContents(true);
pMovie->start();
connect(ui->pbtnJump2,SIGNAL(clicked()),this,SLOT(vBtnJump2Process()));
connect(ui->pbtnJump3,SIGNAL(clicked()),this,SLOT(vBtnJump3Process()));
this->pPage2=new twowidget;
connect(this->pPage2,SIGNAL(signalBtnPage1(int)),this,SLOT(vBtnJump1Process(int)));
connect(this->pPage2,SIGNAL(signalBtnPage3()),this,SLOT(vBtnJump3Process()));
this->pPage3=new threeWidget;
connect(this->pPage3,SIGNAL(vSingalPage1(int)),this,SLOT(vBtnJump1Process(int)));
connect(this->pPage3,SIGNAL(vSingalPage2()),this,SLOT(vBtnJump2Process()));
}
void OneWidget::vBtnJump3Process()
{
this->pPage3->show();
this->pPage2->hide();
this->hide();
}
void OneWidget::vBtnJump2Process()
{
this->pPage2->show();
this->pPage3->hide();
this->hide();
qDebug()<<"press"<<endl;
}
void OneWidget::vBtnJump1Process(int slClickCnt)
{
qDebug()<<"vBtnJump1Process "<<slClickCnt<<endl;
this->show();
this->pPage2->hide();
this->pPage3->hide();
}
twowidget.h
#ifndef TWOWIDGET_H
#define TWOWIDGET_H
#include <QWidget>
namespace Ui {
class twowidget;
}
class twowidget : public QWidget
{
Q_OBJECT
public:
explicit twowidget(QWidget *parent = nullptr);
~twowidget();
private:
Ui::twowidget *ui;
void vInit(void);
int slClickCnt=0;
private slots:
void vBtnPage1Process(void);
void vBtnPage3Process(void);
signals:
void signalBtnPage1(int);
void signalBtnPage3(void);
};
#endif // TWOWIDGET_H
twowidget.cpp
#include "twowidget.h"
#include "ui_twowidget.h"
#include <QMovie>
#include <QDebug>
twowidget::twowidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::twowidget)
{
ui->setupUi(this);
this->vInit();
}
twowidget::~twowidget()
{
delete ui;
}
void twowidget::vInit()
{
this->move(0,0);
this->setWindowFlag(Qt::FramelessWindowHint);
QMovie *pMovie=new QMovie("./dan2.gif");
ui->label->setMovie(pMovie);
ui->label->setScaledContents(true);
pMovie->start();
connect(ui->pbtnJump1,SIGNAL(clicked()),this,SLOT(vBtnPage1Process()));
connect(ui->pbtnJump3,SIGNAL(clicked()),this,SLOT(vBtnPage3Process()));
}
void twowidget::vBtnPage1Process()
{
this->slClickCnt++;
emit signalBtnPage1(this->slClickCnt);
qDebug()<<"emit signal btn page1 "<<this->slClickCnt<<endl;
}
void twowidget::vBtnPage3Process()
{
emit signalBtnPage3();
qDebug()<<"emit signal btn page3 "<<endl;
}
threeWidget.h:
#ifndef THREEWIDGET_H
#define THREEWIDGET_H
#include <QWidget>
#include <QTimer>
namespace Ui {
class threeWidget;
}
class threeWidget : public QWidget
{
Q_OBJECT
public:
explicit threeWidget(QWidget *parent = nullptr);
~threeWidget();
private:
Ui::threeWidget *ui;
void vInit();
int slLcdCnt=0;
int slPage3Cnt=0;
QTimer *pTimer=nullptr;
private slots:
void vBtnPage1Process();
void vBtnPage2Process();
void vTimerProcess();
signals:
void vSingalPage1(int);
void vSingalPage2(void);
};
#endif // THREEWIDGET_H
threeWidget.cpp
#include "threewidget.h"
#include "ui_threewidget.h"
#include <QMovie>
threeWidget::threeWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::threeWidget)
{
ui->setupUi(this);
this->vInit();
}
threeWidget::~threeWidget()
{
delete ui;
}
void threeWidget::vInit()
{
this->move(0,0);
this->setWindowFlag(Qt::FramelessWindowHint);
QMovie *pMovie=new QMovie("./pland.gif");
ui->label->setMovie(pMovie);
ui->label->setScaledContents(true);
pMovie->start();
connect(ui->pbtnJump1,SIGNAL(clicked()),this,SLOT(vBtnPage1Process()));
connect(ui->pbtnJump2,SIGNAL(clicked()),this,SLOT(vBtnPage2Process()));
this->pTimer=new QTimer(this);
connect(this->pTimer,SIGNAL(timeout()),this,SLOT(vTimerProcess()));
this->pTimer->start(100);
}
void threeWidget::vBtnPage1Process()
{
this->slPage3Cnt++;
emit vSingalPage1(this->slPage3Cnt);
}
void threeWidget::vBtnPage2Process()
{
emit vSingalPage2();
}
void threeWidget::vTimerProcess()
{
this->slLcdCnt++;
if(this->slLcdCnt>99)
{
this->slLcdCnt=0;
}
ui->lcdNumber->display(this->slLcdCnt);
}