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

结论

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

相关推荐
心情好的小球藻7 分钟前
Python应用进阶DAY9--类型注解Type Hinting
开发语言·python
惜.己19 分钟前
使用python读取json数据,简单的处理成元组数组
开发语言·python·测试工具·json
Y40900125 分钟前
C语言转Java语言,相同与相异之处
java·c语言·开发语言·笔记
古月-一个C++方向的小白6 小时前
C++11之lambda表达式与包装器
开发语言·c++
沐知全栈开发6 小时前
Eclipse 生成 jar 包
开发语言
杭州杭州杭州7 小时前
Python笔记
开发语言·笔记·python
tanyongxi668 小时前
C++ AVL树实现详解:平衡二叉搜索树的原理与代码实现
开发语言·c++
阿葱(聪)9 小时前
java 在k8s中的部署流程
java·开发语言·docker·kubernetes
浮生带你学Java9 小时前
2025Java面试题及答案整理( 2025年 7 月最新版,持续更新)
java·开发语言·数据库·面试·职场和发展
斯是 陋室9 小时前
在CentOS7.9服务器上安装.NET 8.0 SDK
运维·服务器·开发语言·c++·c#·云计算·.net