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

结论

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

相关推荐
草莓熊Lotso10 分钟前
C++ 手写 List 容器实战:从双向链表原理到完整功能落地,附源码与测试验证
开发语言·c++·链表·list
无限进步_12 分钟前
【C语言】杨辉三角:数学之美与编程实现的完美结合
c语言·开发语言
Cg1362691597414 分钟前
封装的实现和定义
java·开发语言
武子康19 分钟前
Java-146 深入浅出 MongoDB 数据插入、批量写入、BSON 格式与逻辑查询and or not操作指南
java·开发语言·数据库·sql·mongodb·性能优化·nosql
初圣魔门首席弟子22 分钟前
const string getWord() ;和 string getWord() const ;是一样的效果吗
开发语言·c++
星源~26 分钟前
Linux-Ubuntu系统安装特别指导
linux·qt·ubuntu·嵌入式开发·物联网设备
lly20240635 分钟前
Docker 安装 Node.js
开发语言
明天会有多晴朗41 分钟前
C语言入门教程(第6讲):函数——让程序学会“分工合作”的魔法
c语言·开发语言·算法
泽虞1 小时前
《Qt应用开发》笔记p3
linux·开发语言·数据库·c++·笔记·qt·面试
晓风残月淡1 小时前
JVM字节码与类的加载(一):类的加载过程详解
开发语言·jvm·python