QT中QStackedWidget控件功能及应用

一.概述

1.介绍

QStackedWidget 是 Qt 中一个非常重要的容器控件,它允许你在同一区域显示多个页面(子部件),但每次只显示其中一个页面。

StackedWidget 是 Qt 中实现多页面应用的强大工具,特别适合需要复杂页面切换逻辑的场景。

2.核心特性

二.代码示例

1.基本功能

QVBoxLayout *mainLayout = new QVBoxLayout(this);

// 创建 Stacked Widget

QStackedWidget *stackedWidget = new QStackedWidget(this);

stackedWidget->setGeometry(50, 150, 100, 100); //控件绝对位置,长、宽

// 创建多个页面并添加到 Stacked Widget

// 页面1:欢迎页面

QWidget *page1 = new QWidget(this);

QVBoxLayout *layout1 = new QVBoxLayout(page1);

layout1->addWidget(new QLabel("欢迎使用我的应用!"));

QPushButton *toPage2Btn = new QPushButton("前往设置");

layout1->addWidget(toPage2Btn);

stackedWidget->addWidget(page1); // 索引0

// 页面2:设置页面

QWidget *page2 = new QWidget(this);

page2->setStyleSheet("background-color: green;" );// 背景色

QVBoxLayout *layout2 = new QVBoxLayout(page2);

layout2->addWidget(new QLabel("应用设置"));

QPushButton *toPage1Btn = new QPushButton(this);

toPage1Btn->setText("返回首页");

layout2->addWidget(toPage1Btn);

stackedWidget->addWidget(page2); // 索引1

// 页面3:关于页面

QWidget *page3 = new QWidget(this);

page3->setStyleSheet("background-color: red;" );// 背景色

QVBoxLayout *layout3 = new QVBoxLayout(page3);

layout3->addWidget(new QLabel("关于我们"));

QPushButton *toPage1Btn2 = new QPushButton(this);

toPage1Btn2->setText("返回首页1");

layout3->addWidget(toPage1Btn2);

stackedWidget->addWidget(page3); // 索引2

// 添加控制按钮

QHBoxLayout *controlLayout = new QHBoxLayout(this);

QPushButton *prevBtn = new QPushButton(this);

prevBtn->setGeometry(250, 150, 80, 50); //控件绝对位置,长、宽

prevBtn->setText("上一页");

QPushButton *nextBtn = new QPushButton(this);

nextBtn->setGeometry(350, 150, 80, 50); //控件绝对位置,长、宽

nextBtn->setText("下一页");

controlLayout->addWidget(prevBtn);

controlLayout->addWidget(nextBtn);

// 组装界面

mainLayout->addWidget(stackedWidget);

mainLayout->addLayout(controlLayout);

// 连接信号槽

QObject::connect(toPage2Btn, &QPushButton::clicked, stackedWidget() {

stackedWidget->setCurrentIndex(1); // 切换到设置页面

});

QObject::connect(toPage1Btn, &QPushButton::clicked, stackedWidget() {

stackedWidget->setCurrentIndex(0); // 切换回首页

});

QObject::connect(toPage1Btn2, &QPushButton::clicked, stackedWidget() {

stackedWidget->setCurrentIndex(0); // 切换回首页

});

QObject::connect(prevBtn, &QPushButton::clicked, stackedWidget() {

int current = stackedWidget->currentIndex();

int count = stackedWidget->count();

stackedWidget->setCurrentIndex((current - 1 + count) % count);

});

QObject::connect(nextBtn, &QPushButton::clicked, stackedWidget() {

int current = stackedWidget->currentIndex();

int count = stackedWidget->count();

stackedWidget->setCurrentIndex((current + 1) % count);

});

2.常用方法详解

(1)页面管理方法

// 添加页面

int index1 = stackedWidget->addWidget(pageWidget); // 返回页面索引

// 插入页面

int index2 = stackedWidget->insertWidget(1, anotherWidget); // 在指定位置插入

// 移除页面(不删除部件)

stackedWidget->removeWidget(pageWidget);

// 获取页面数量

int pageCount = stackedWidget->count();

// 获取指定索引的页面

QWidget *widget = stackedWidget->widget(2);

// 获取页面索引

int index = stackedWidget->indexOf(pageWidget);

(2)页面切换方法

// 通过索引切换

stackedWidget->setCurrentIndex(2);

// 通过部件指针切换

stackedWidget->setCurrentWidget(pageWidget);

// 获取当前页面索引

int currentIndex = stackedWidget->currentIndex();

// 获取当前页面部件

QWidget *currentWidget = stackedWidget->currentWidget();

三.与 QTabWidget 的对比

相关推荐
热心网友俣先生3 分钟前
2026年华数杯C 题 超详细解题思路
c语言·开发语言·人工智能
啄缘之间16 分钟前
5.1 uvm‑component 的层级结构管理? new (name,parent) 参数
开发语言·笔记·学习·ic·uvm·sv
旖旎夜光31 分钟前
C++(类与对象)(下)
开发语言·c++·学习
张小凡vip37 分钟前
Python爬虫实战:高效稳定的文件下载模块设计与实现
开发语言·爬虫·python
江屿风38 分钟前
【科普】【差集&交集概念落地云相册】流食般投喂
开发语言·c++·笔记·算法·云相册
代龙涛41 分钟前
WordPress header.php 与 footer.php 模板结构详解
开发语言·php·wordpress
黄贵根6 小时前
JavaScript实现教培行业意向登记系统
开发语言·javascript·ecmascript
zzzll11119 小时前
Claude Code离线安装方案揭秘
开发语言·php
wling030110 小时前
vasp虚频计算-python脚本
开发语言·windows·python·vasp计算
郝学胜-神的一滴10 小时前
Qt 高级编程 040:按钮悬浮弹出滑块弹窗的完整攻略
开发语言·c++·qt·软件工程·用户界面