• Qt 堆栈窗体QStackedWidget使用


    QStackedWidget控件相当于一个容器,提供一个空间来存放一系列的控件,并且每次只能有一个控件是可见的,即被设置为当前的控件。QStackedWidget可用于创建类似于QTabWidget提供的用户界面。 它是一个构建在QStackedLayout类之上的方便布局小部件。

    在这里插入图片描述

    方法

    • int addWidget(QWidget *widget) 添加页面,并返回页面对应的索引
    • int count() const 获取页面数量
    • int currentIndex() const 获取当前页面的索引
    • QWidget* currentWidget() const 获取当前页面
    • int indexOf(QWidget *widget) const 获取QWidget页面所对应的索引。可以用来判断QWidget是否存在QStackedWidget中,不存在返回值为-1。
    • int insertWidget(int index, QWidget *widget) 在索引index位置添加页面
    • void removeWidget(QWidget *widget) 移除QWidget页面,并没有被删除,只是从布局中移动,从而被隐藏。
    • QWidget* widget(int index) const 获取索引index所对应的页面

    信号

    • void currentChanged(int index) 当前页面发生变化时候发射,index为新的索引值
    • void widgetRemoved(int index) 页面被移除时候发射,index为页面对应的索引值

    槽函数

    • void setCurrentIndex(int index) 设置索引index所在的页面为当前页面
    • void setCurrentWidget(QWidget *widget) 设置QWidget页面为当前页面
    #pragma once
    #include 
    #include "ui_stackedwidget.h"
    #include "common/stylemgr.h"
    
    class StackedWidget : public QWidget
    {
      Q_OBJECT
    private:
      Ui_stackedwidget *ui;
      QWidget *aqwidget = nullptr;
    
    public:
      StackedWidget(QWidget *parent = nullptr);
      ~StackedWidget();
    
    public slots:
      void AddAWidget();
      void ShowAWidget();
      void DelAWidget();
      void ShowIndex0();
      void ShowCurrentChanged(int index);
      void ShowWidgetRemoved(int index);
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    #include "stackedwidget.h"
    
    StackedWidget::StackedWidget(QWidget *parent) : QWidget(parent), ui(new Ui_stackedwidget())
    {
      ui->setupUi(this);
      StyleMgr::SetStyleToWidgetByCssFile(this, ":/helloqt/resources/qss/custom/stackedwidget.qss");
      QStringList list;
      list << "one page"
           << "two page"
           << "third page";
      ui->stackedwidget_comboBox->addItems(list);
      connect(ui->stackedwidget_comboBox, QOverload<int>::of(&QComboBox::activated), ui->stackedwidget_stackedWidget, &QStackedWidget::setCurrentIndex);
      connect(ui->stackedwidget_stackedWidget, QOverload<int>::of(&QStackedWidget::currentChanged), this, ShowCurrentChanged);
      connect(ui->stackedwidget_stackedWidget, QOverload<int>::of(&QStackedWidget::widgetRemoved), this, ShowWidgetRemoved);
    
      connect(ui->stackedwidget_pushButton_1, &QPushButton::clicked, this, AddAWidget);
      connect(ui->stackedwidget_pushButton_2, &QPushButton::clicked, this, ShowAWidget);
      connect(ui->stackedwidget_pushButton_3, &QPushButton::clicked, this, DelAWidget);
      connect(ui->stackedwidget_pushButton_4, &QPushButton::clicked, this, ShowIndex0);
    }
    
    StackedWidget::~StackedWidget()
    {
      delete ui;
    }
    
    void StackedWidget::AddAWidget()
    {
      if (aqwidget == nullptr)
      {
        aqwidget = new QWidget(ui->stackedwidget_stackedWidget);
        aqwidget->setObjectName("stackedwidget_page_a");
      }
    
      int index = ui->stackedwidget_stackedWidget->indexOf(aqwidget);
      qDebug() << index;
      if (index < 0)
      {
        ui->stackedwidget_stackedWidget->addWidget(aqwidget);
      }
    }
    
    void StackedWidget::ShowAWidget()
    {
      if (aqwidget != nullptr)
      {
        ui->stackedwidget_stackedWidget->setCurrentWidget(aqwidget);
      }
    }
    
    void StackedWidget::DelAWidget()
    {
      if (aqwidget != nullptr)
      {
        ui->stackedwidget_stackedWidget->removeWidget(aqwidget);
      }
    }
    
    void StackedWidget::ShowIndex0()
    {
      ui->stackedwidget_stackedWidget->setCurrentIndex(0);
    }
    
    void StackedWidget::ShowCurrentChanged(int index)
    {
      QMessageBox msgBox;
      msgBox.setText(QString("ShowCurrentChanged index %1").arg(index));
      msgBox.exec();
    }
    
    void StackedWidget::ShowWidgetRemoved(int index)
    {
      QMessageBox msgBox;
      msgBox.setText(QString("ShowWidgetRemoved index %1").arg(index));
      msgBox.exec();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77

    效果

    在这里插入图片描述
    在这里插入图片描述

    github

    https://github.com/weichangk/helloqt

    参考

    https://doc.qt.io/qt-5/qstackedwidget.html

  • 相关阅读:
    C 标准库 - <stdio.h> 详解
    手撕 视觉slam14讲 ch13 代码(6)正常跟踪模式 Track()
    【论文阅读】Directional Connectivity-based Segmentation of Medical Images
    算法作业1-2 字典序问题
    企业集中监控体系思路及架构
    【Git教程】(六)分支合并 —— 合并过程,各类合并冲突及解决思路 ~
    北峰多层级融合通信解决方案,搭建调度“一张图”通信网络
    css控制整个div下的所有元素中的文字放大缩小
    微软发现影响 Linux 和 macOS系统的 ncurses 库漏洞
    字符串拼接你真的啥都知道了吗
  • 原文地址:https://blog.csdn.net/qq_39827640/article/details/127990399