博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Qt之QStackedWidget
阅读量:4166 次
发布时间:2019-05-26

本文共 1676 字,大约阅读时间需要 5 分钟。

接口

  • int addWidget(QWidget * widget)

    添加页面,并返回页面对应的索引

  • int count() const

    获取页面数量

  • int currentIndex() const

    获取当前页面的索引

  • QWidget * currentWidget() const

    获取当前页面

  • int indexOf(QWidget * widget) const

    获取QWidget页面所对应的索引

  • 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页面为当前页面

总结

一般情况,常用的两种方式:

  • 根据currentWidget()来判断当前页面,然后通过setCurrentWidget()来设置需要显示的页面。

  • 根据currentIndex()来判断当前页面索引,然后通过setCurrentIndex()来设置需要显示的页面。

QPushButton *pButton = new QPushButton(this);QLabel *pFirstPage = new QLabel(this);QLabel *pSecondPage = new QLabel(this);QLabel *pThirdPage = new QLabel(this);m_pStackedWidget = new QStackedWidget(this);pButton->setText(QStringLiteral("点击切换"));pFirstPage->setText(QStringLiteral("一去丶二三里"));pSecondPage->setText(QStringLiteral("青春不老,奋斗不止!"));pThirdPage->setText(QStringLiteral("纯正开源之美,有趣、好玩、靠谱。。。"));m_pStackedWidget->addWidget(pFirstPage);m_pStackedWidget->addWidget(pSecondPage);m_pStackedWidget->addWidget(pThirdPage);pButton->setGeometry(100, 100, 200, 50);m_pStackedWidget->setGeometry(150, 150, 200, 200);connect(pButton, &QPushButton::clicked, this, &Widget::switchPage);void Widget::switchPage(){	int nCount = m_pStackedWidget->count();	int nIndex = m_pStackedWidget->currentIndex();	// 获取下一个需要显示的页面索引	++nIndex;	// 当需要显示的页面索引大于等于总页面时,切换至首页	if (nIndex >= nCount)		nIndex = 0;	m_pStackedWidget->setCurrentIndex(nIndex);}

转载地址:http://ctqxi.baihongyu.com/

你可能感兴趣的文章
SpringBoot之快速部署
查看>>
springBoot之jar包在后台(运行:编写start、stop脚本)
查看>>
redis学习
查看>>
SpringBoot之application.properties文件能配置的属性
查看>>
javaWeb监听器、过滤器、拦截器
查看>>
RESTFUL风格的接口
查看>>
后台参数验证配置
查看>>
SpringBoot之外置Tomcat配置
查看>>
java 删除 list 中的元素
查看>>
idea启动优化
查看>>
java发展史
查看>>
Java内存区域
查看>>
数据库与模式的区别
查看>>
Sql随机取数据
查看>>
PHP定时跳转
查看>>
include、require、include_once、require_once的区别
查看>>
构造函数、析构函数是否要声明为虚函数的问题
查看>>
C++中的虚函数
查看>>
Mysql数据库创建用户
查看>>
一套华为面试题
查看>>