深入浅出之QStackWidget

QStackWidget并非Qt框架中的一个标准控件,你可能是指的是QStackedWidget。以下是对QStackedWidget的详细介绍:

一、概述

QStackedWidget是Qt框架中的一个非常有用的控件,它允许你堆叠多个窗口部件(widgets),但一次只显示一个。这种机制非常适合于实现向导、多视图应用程序、选项卡界面(虽然它没有内置的选项卡头)以及表单向导等场景。通过改变当前索引,可以轻松地切换显示的窗口部件。

二、功能特点

  1. 页面堆叠:QStackedWidget通过堆叠的方式管理多个页面(子部件),每个页面都有一个唯一的索引值。只有当前索引对应的页面是可见的,其他页面则会被隐藏。
  2. 动态切换:开发者可以通过设置当前索引或当前子部件来动态地切换显示的页面。这种切换是即时的,且可以响应各种事件或用户操作。
  3. 灵活布局:QStackedWidget本身并不提供布局管理功能,但它可以与其他布局管理器(如QVBoxLayout、QHBoxLayout等)结合使用,以实现复杂的界面布局。

三、常用函数

  1. addWidget(QWidget *widget):向QStackedWidget中添加一个新页面,并将其添加到内部页面列表中。这个方法会自动为新页面分配一个索引。
  2. insertWidget(int index, QWidget *widget):在指定索引处插入一个新页面。如果索引超出了当前页面范围,页面将被添加到末尾。
  3. removeWidget(QWidget *widget):从QStackedWidget中移除指定的页面。页面本身不会被删除,只是从堆叠中移除。
  4. currentIndex():返回当前显示的页面的索引。
  5. setCurrentIndex(int index):设置当前显示的页面为指定索引的页面。
  6. currentWidget():返回当前显示的页面的QWidget指针。
  7. widget(int index):返回指定索引处的页面的QWidget指针。
  8. count():返回QStackedWidget中页面的总数。

四、重要信号

  1. currentChanged(int index):当当前显示页面的索引改变时发射。
  2. widgetRemoved(int index):当某个页面被移除时发射,根据索引定位。

五、QStackWidget实现透明化 

    pageStackWidget = new QStackedWidget(this);
    QGraphicsOpacityEffect *m_pGraphicsOpacityEffect = new QGraphicsOpacityEffect(this);
    pageStackWidget->setGraphicsEffect(m_pGraphicsOpacityEffect );
    m_pGraphicsOpacityEffect->setOpacity(0.5);
 
    loadingPage = new QWidget(pageStackWidget);
    completePage = new QWidget(pageStackWidget);
    errorPage = new QWidget(pageStackWidget);
 
 
    confirmButton = new QPushButton(completePage);
    confirmButton->setFocusPolicy(Qt::NoFocus);
    confirmButton->setText(tr("OK"));
    confirmButton->resize(80,40);
    completePhotoLabel = new QLabel(completePage);
    completePhotoLabel->resize(80,100);
    completePhotoLabel->move(this->width()/2-completePhotoLabel->width()/2,20);
    confirmButton->move(this->width()/2-confirmButton->width()/2,150);
    completePhotoLabel->setPixmap(QPixmap(":Resource/photo/complete.png"));
    completePhotoLabel->setAlignment(Qt::AlignCenter);
 
    errorButton = new QPushButton(errorPage);
    errorButton->setText(tr("OK"));
    errorButton->resize(80,40);
    errorPhotoLabel = new QLabel(errorPage);
    errorPhotoLabel->resize(80,100);
    errorPhotoLabel->move(this->width()/2-errorPhotoLabel->width()/2,20);
    errorButton->move(this->width()/2-errorButton->width()/2,150);
    errorPhotoLabel->setPixmap(QPixmap(":Resource/photo/error.png"));
    errorPhotoLabel->setAlignment(Qt::AlignCenter);
 
    cmovieLabel = new CMoviceLabel(loadingPage);
    pageStackWidget->addWidget(loadingPage);
    pageStackWidget->addWidget(completePage);
    pageStackWidget->addWidget(errorPage);
 
    pageStackWidget->setGeometry(0,0,320,240);
    loadingPage->setGeometry(0,0,320,240);
    completePage->setGeometry(0,0,320,240);
    errorPage->setGeometry(0,0,320,240);
 
    cmovieLabel->move(0,0);
    pageStackWidget->setCurrentWidget(loadingPage);

六、使用示例

以下是一个简单的使用QStackedWidget的示例代码:

#include <QApplication>
#include <QMainWindow>
#include <QStackedWidget>
#include <QWidget>
#include <QVBoxLayout>
#include <QLabel>
#include <QPushButton>

class MainWindow : public QMainWindow {
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr) : QMainWindow(parent) {
        setWindowTitle("QStackedWidget Example");
        resize(400, 300);

        // 创建一个QStackedWidget
        QStackedWidget *stackedWidget = new QStackedWidget(this);

        // 创建第一个页面并设置内容
        QWidget *page1 = new QWidget();
        QVBoxLayout *layout1 = new QVBoxLayout(page1);
        QLabel *label1 = new QLabel("This is the first page.", page1);
        layout1->addWidget(label1);

        // 创建第二个页面并设置内容
        QWidget *page2 = new QWidget();
        QVBoxLayout *layout2 = new QVBoxLayout(page2);
        QLabel *label2 = new QLabel("This is the second page.", page2);
        layout2->addWidget(label2);

        // 创建第三个页面并设置内容
        QWidget *page3 = new QWidget();
        QVBoxLayout *layout3 = new QVBoxLayout(page3);
        QLabel *label3 = new QLabel("This is the third page.", page3);
        layout3->addWidget(label3);

        // 向QStackedWidget中添加页面
        stackedWidget->addWidget(page1);
        stackedWidget->addWidget(page2);
        stackedWidget->addWidget(page3);

        // 创建一个按钮栏来切换页面
        QWidget *buttonBar = new QWidget(this);
        QHBoxLayout *buttonLayout = new QHBoxLayout(buttonBar);
        QPushButton *button1 = new QPushButton("Page 1", buttonBar);
        QPushButton *button2 = new QPushButton("Page 2", buttonBar);
        QPushButton *button3 = new QPushButton("Page 3", buttonBar);
        buttonLayout->addWidget(button1);
        buttonLayout->addWidget(button2);
        buttonLayout->addWidget(button3);

        // 连接按钮点击信号到槽函数来切换页面
        connect(button1, &QPushButton::clicked, this, [stackedWidget]() {
            stackedWidget->setCurrentIndex(0);
        });
        connect(button2, &QPushButton::clicked, this, [stackedWidget]() {
            stackedWidget->setCurrentIndex(1);
        });
        connect(button3, &QPushButton::clicked, this, [stackedWidget]() {
            stackedWidget->setCurrentIndex(2);
        });

        // 创建一个中心部件并设置布局
        QWidget *centralWidget = new QWidget(this);
        QVBoxLayout *mainLayout = new QVBoxLayout(centralWidget);
        mainLayout->addWidget(buttonBar);
        mainLayout->addWidget(stackedWidget);

        // 将中心部件设置为主窗口的部件
        setCentralWidget(centralWidget);
    }
};

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);

    // 创建并显示主窗口
    MainWindow mainWindow;
    mainWindow.show();

    // 进入应用程序的主事件循环
    return app.exec();
}

参考:

  1. Qt QStackWidget实现透明化loading弹窗与结果展示_qstackedwidget 设置透明-CSDN博客
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

浩瀚之水_csdn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值