【Qt】QStackedWidget、QRadioButton、QPushButton及布局实现程序首页自动展示功能

效果

在程序启动后,有时不会进入到工作页面,会进入到产品展示页面。

动画如下:

首页展示

页面操作

  • 当不点击时,一秒自动刷新一次;
  • 当点击时,会自动跳转到对应页面;
  • 点击上一页、下一页、及跳转页数,会自动跳转。

UI设计

示例

注释在代码中,代码可运行。
.h

cpp 复制代码
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

#include <QMap>
#include <QRadioButton>
#include <QButtonGroup>
namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();


private:
    Ui::MainWindow *ui;
	// 用于管理选择按钮
    QMap<int, QRadioButton*>    m_mapRadios;
    // 加入组
    QButtonGroup*               m_pBtnGroup;
};

#endif // MAINWINDOW_H

.cpp

cpp 复制代码
#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QDebug>
#include <QLabel>
#include <QFont>

#include <QTimer>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    
    ui->pushButton->setStyleSheet("QPushButton {background-color: #eeeeee; outline: none;}");
    ui->pushButton_2->setStyleSheet("QPushButton {background-color: #eeeeee; outline: none;}");

    m_pBtnGroup = new QButtonGroup(this);

    ui->horizontalLayout->addStretch();
    // 循环创建按钮
    for (int var = 0; var < 5; ++var) {
        QRadioButton* pR = new QRadioButton(QString::number(var+1));
        m_mapRadios.insert(var, pR);

        ui->horizontalLayout->addWidget(pR);
        // 加入组
        m_pBtnGroup->addButton(pR, var);
    }
    ui->horizontalLayout->addStretch();
    // 加入map
    m_mapRadios.value(m_mapRadios.keys().first())->setChecked(true);

    // 循环创建展示界面
    for (int var = 0; var < 5; ++var) {
        // 设置信息
        QLabel* pLabel = new QLabel(this);
        pLabel->setText(QString("第%1页面").arg(var+1));
        pLabel->resize(150, 100);
        QFont font = pLabel->font();
        font.setPointSize(23);
        font.setBold(true);
        pLabel->setFont(font);
        pLabel->setStyleSheet("QLabel{color:#ff0000;}");
        pLabel->move(width()/2, height()/2);
        ui->stackedWidget->insertWidget(ui->stackedWidget->count(), pLabel);
    }
    ui->stackedWidget->setCurrentIndex(0);

    // 根据点击按钮,同步展示页面
    connect(m_pBtnGroup, static_cast<void (QButtonGroup::*)(int)>(&QButtonGroup::buttonClicked), this, [=](int nId){
        ui->stackedWidget->setCurrentIndex(nId);
    });
    // 定时器
    QTimer* pTimer = new QTimer(this);
    connect(pTimer, &QTimer::timeout, this, [=](){
        // 每一秒更新一次
       int nIndex = ui->stackedWidget->currentIndex();
       if(nIndex < 4)
       {
           ui->stackedWidget->setCurrentIndex(nIndex+1);
           m_mapRadios.value(nIndex+1)->setChecked(true);
       }else{
           ui->stackedWidget->setCurrentIndex(0);
           m_mapRadios.value(0)->setChecked(true);
       }
    });

    connect(ui->pushButton, &QPushButton::clicked, this, [=](){
        int nIndex = ui->stackedWidget->currentIndex();
        if(nIndex > 0)
        {
            ui->stackedWidget->setCurrentIndex(nIndex-1);
            m_mapRadios.value(nIndex-1)->setChecked(true);
        }
    });
    connect(ui->pushButton_2, &QPushButton::clicked, this, [=](){
        int nIndex = ui->stackedWidget->currentIndex();
        if(nIndex < 4)
        {
            ui->stackedWidget->setCurrentIndex(nIndex+1);
            m_mapRadios.value(nIndex+1)->setChecked(true);
        }
    });

    pTimer->start(1000);

}

MainWindow::~MainWindow()
{
    delete ui;
}

结论

可将每页显示的改为产品或者介绍图,实现业务。

相关推荐
XXYBMOOO几秒前
基于 Qt 的 BMP 图像数据存取至 SQLite 数据库的实现
数据库·c++·qt
Susea&37 分钟前
数据结构初阶:双向链表
c语言·开发语言·数据结构
pianmian11 小时前
arcgis几何与游标(1)
开发语言·python
-曾牛1 小时前
【LangChain4j快速入门】5分钟用Java玩转GPT-4o-mini,Spring Boot整合实战!| 附源码
java·开发语言·人工智能·spring boot·ai·chatgpt
nanzhuhe2 小时前
python中参数前**的含义
开发语言·python
wt_cs2 小时前
身份认证C#集成方案-数字时代身份证实名认证利器
开发语言·c#
ghost1432 小时前
Python自学第2天:条件语句,循环语句
开发语言·python·学习
Chandler242 小时前
Go:低级编程
开发语言·后端·golang
^_^ 纵歌2 小时前
用python比较两个mp4是否实质相同
开发语言·python·音频·视频
一直走下去-明2 小时前
使用python帮助艺术家完成角色动画和服装模型等任务
开发语言·图像处理·pytorch·python·opencv·ai作画