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

结论

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

相关推荐
axban1 小时前
QT M/V架构开发实战:QFileSystemModel介绍
开发语言·qt·架构
钢门狂鸭3 小时前
关于rust的crates.io
开发语言·后端·rust
Lionel_SSL4 小时前
《深入理解Java虚拟机》第三章读书笔记:垃圾回收机制与内存管理
java·开发语言·jvm
技术猿188702783514 小时前
PHP 与 WebAssembly 的 “天然隔阂”
开发语言·php·wasm
薄荷撞~可乐4 小时前
C#Task(Api)应用
开发语言·c#
another heaven6 小时前
【Qt VS2022调试时无法查看QString等Qt变量信息】解决方法
开发语言·qt
A黄俊辉A6 小时前
axios+ts封装
开发语言·前端·javascript
杨福瑞7 小时前
C语⾔内存函数
c语言·开发语言
eqwaak07 小时前
科技信息差(9.12)
开发语言·python·科技·量子计算
axban7 小时前
QT M/V架构开发实战:QStringListModel介绍
开发语言·数据库·qt