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

结论

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

相关推荐
iCxhust1 小时前
c# U盘映像生成工具
开发语言·单片机·c#
yangzhi_emo2 小时前
ES6笔记2
开发语言·前端·javascript
emplace_back2 小时前
C# 集合表达式和展开运算符 (..) 详解
开发语言·windows·c#
jz_ddk3 小时前
[学习] C语言数学库函数背后的故事:`double erf(double x)`
c语言·开发语言·学习
萧曵 丶3 小时前
Rust 所有权系统:深入浅出指南
开发语言·后端·rust
xiaolang_8616_wjl3 小时前
c++文字游戏_闯关打怪2.0(开源)
开发语言·c++·开源
收破烂的小熊猫~3 小时前
《Java修仙传:从凡胎到码帝》第四章:设计模式破万法
java·开发语言·设计模式
nananaij3 小时前
【Python进阶篇 面向对象程序设计(3) 继承】
开发语言·python·神经网络·pycharm
阿蒙Amon4 小时前
为什么 12 版仍封神?《C# 高级编程》:从.NET 5 到实战架构,进阶者绕不开的必修课
开发语言·c#