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 <QtWidgets>
#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);
};
#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();
}

效果

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

github

https://github.com/weichangk/helloqt

参考

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值