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 的对比

相关推荐
老歌老听老掉牙2 小时前
解决 PyQt5 中 sipPyTypeDict() 弃用警告的完整指南
python·qt
兜有米啦3 小时前
python练习题3
开发语言·python
Wzx1980123 小时前
go基础语法练习
开发语言·后端·golang
忧郁的蛋~3 小时前
.NET异步编程中内存泄漏的终极解决方案
开发语言·前端·javascript·.net
2301_795167203 小时前
玩转Rust高级应用. ToOwned trait 提供的是一种更“泛化”的Clone 的功能,Clone一般是从&T类型变量创造一个新的T类型变量
开发语言·后端·rust
你才是向阳花3 小时前
如何用Python实现飞机大战小游戏
开发语言·python·pygame
合作小小程序员小小店3 小时前
web网页开发,在线%商城,电商,商品购买%系统demo,基于vscode,apache,html,css,jquery,php,mysql数据库
开发语言·前端·数据库·mysql·html·php·电商
星释4 小时前
Rust 练习册 :Phone Number与电话号码处理
开发语言·机器学习·rust
one year.4 小时前
Linux:线程同步与互斥
java·开发语言
一 乐4 小时前
旅游|内蒙古景点旅游|基于Springboot+Vue的内蒙古景点旅游管理系统设计与实现(源码+数据库+文档)
开发语言·前端·数据库·vue.js·spring boot·后端·旅游