C++,QT多个窗体切换,widget 多个页面切换跳转显示

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);
}

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Qt是一款广泛应用于GUI界面开发的跨平台框架,它提供了许多实用的控件和功能。在Qt中实现右侧圆点页面切换,需要用到QStackedWidget和QPushButton控件。 首先,我们创建一个QStackedWidget控件,用于存放不同的页面。然后,在设计界面中添加多个QPushButton控件,并设置它们的样式为圆点。在按钮的clicked()信号中,我们通过代码切换到对应的页面。 具体实现方式如下: 1.在Qt设计师中添加QStackedWidget控件,并在其中添加多个QWidget页面。 2.在QStackedWidget中,右键选择“在QStackedWidget中添加Widget”,为每个页面添加一个独立的QWidget控件。 3.在页面上添加多个QPushButton控件,并将它们的样式设置为圆点。 4.在代码中绑定按钮的clicked()信号,通过QStackedWidget的setCurrentIndex函数切换页面。 示例代码如下: //Button1、Button2、Button3为个QPushButton控件 //stackedWidget为QStackedWidget控件 connect(ui->Button1, SIGNAL(clicked()), this, SLOT(on_Button1_clicked())); connect(ui->Button2, SIGNAL(clicked()), this, SLOT(on_Button2_clicked())); connect(ui->Button3, SIGNAL(clicked()), this, SLOT(on_Button3_clicked())); void MainWindow::on_Button1_clicked(){ stackedWidget->setCurrentIndex(0); } void MainWindow::on_Button2_clicked(){ stackedWidget->setCurrentIndex(1); } void MainWindow::on_Button3_clicked(){ stackedWidget->setCurrentIndex(2); } 最后,我们可以通过添加动画效果和设置按钮状态来完善右侧圆点页面切换的交互体验。 ### 回答2: Qt 中的右侧圆点页面切换是通过控件 QStackedWidgetQTabWidget 实现的。这两个控件都可以将多个页面组织在一起,实现页面切换的功能。 QStackedWidget 是一个简单的容器控件,可以嵌套多个 widget。它的页面切换是通过 setCurrentIndex() 函数和 currentIndexChanged() 信号来实现的。需要将 QStackedWidget 添加到主窗口中,并在其内部添加嵌套的 widget。通过设置 widget 的 index,可以实现页面切换。例如: ```cpp QStackedWidget* stackedWidget = new QStackedWidget(this); stackedWidget->addWidget(page1); stackedWidget->addWidget(page2); stackedWidget->addWidget(page3); stackedWidget->setCurrentIndex(1); // 初始显示第二个页面 connect(stackedWidget, &QStackedWidget::currentIndexChanged, [=](int index){ qDebug() << "页面切换:" << index; }); ``` QTabWidget 是一个带有标签页的控件,可以使用户选择要显示页面。它的使用方式相对简单,在主窗口中添加 QTabWidget 控件,然后将每个标签页添加到 QTabWidget 中。与 QStackedWidget 不同的是,QTabWidget 的标签页可以通过鼠标点击来切换。例如: ```cpp QTabWidget* tabWidget = new QTabWidget(this); tabWidget->addTab(page1, "页面 1"); tabWidget->addTab(page2, "页面 2"); tabWidget->addTab(page3, "页面 3"); connect(tabWidget, &QTabWidget::currentChanged, [=](int index){ qDebug() << "页面切换:" << index; }); ``` 总之,无论是使用 QStackedWidget 还是 QTabWidget,都可以方便地实现右侧圆点页面切换的功能。开发者可以根据具体的需求来选择使用哪个控件。 ### 回答3: Qt右侧圆点页面切换是指,在Qt应用程序中,通过点击圆点来实现不同页面之间的切换。这种功能常见于移动端应用、桌面应用和web网页等。 Qt右侧圆点页面切换的实现方式主要依赖于Qt框架提供的信号和槽机制。通常情况下,每个圆点对应一个页面,当圆点被点击时,我们可以通过设定相应的槽函数来实现页面切换。 具体的步骤如下: 1.在Qt设计师中新建一个窗口,并添加多个页面。 2.在页面上添加多个圆点,并设定每个圆点的属性和槽函数。 3.通过信号和槽机制,将圆点的点击事件与页面切换函数相连接。 4.在页面切换函数中,实现页面显示和隐藏,从而达到页面切换的效果。 除此之外,我们还可以通过设置动画效果、添加滑动手势等方式来增强页面切换的交互体验。 总之,Qt右侧圆点页面切换是一个非常实用的功能,它可以方便用户浏览多个页面,并提高应用程序的易用性和美观性。相信在不久的将来,它将会成为各种Qt应用程序的必备功能之一。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值