当前位置: 首页 > news >正文

【QT】如何查找和获取界面上的子部件(findChild 和 findChidren)

目录

  • 1. findChild()函数
  • 2. findChildren()函数
  • 3. 示例

1. findChild()函数

函数原型:

T QObject::findChild(const QString &name = QString(), Qt::FindChildOptions options = Qt::FindChildrenRecursively) const

返回该对象的子对象,该子对象可以转换为类型T。
T:T为模板,需要查找什么类型就写什么类型。
name:为对象的名称
参数 Qt::FindChildOptions options:
(1)Qt::FindDirectChildrenOnly:只查看对象的直接子对象
(2)Qt::FindChildrenRecursively:查看对象的所有子对象(递归搜索)(默认值)。

例如:返回parentWidget的一个名为"button1"的子QPushButton

 QPushButton *button = parentWidget->findChild<QPushButton *>("button1");

若只有一个QPushButton,其name也可以不用指定

 QPushButton *button = parentWidget->findChild<QPushButton *>();

2. findChildren()函数

函数原型:

QList<T> QObject::findChildren(const QString &name = QString(), Qt::FindChildOptions options = Qt::FindChildrenRecursively) const

返回一个该对象的所有子对象的列表

例如:返回所有父parentWidget中所有的QpushButton

 QList<QPushButton *> parentWidget= parentWidget->findChildren<QPushButton *>();

3. 示例

查找MainWindow的菜单栏、工具栏和状态栏,并进行隐藏

MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent), ui(new Ui::MainWindow) {ui->setupUi(this);QMenuBar* pMenuBar = findChild<QMenuBar*>();              //通过findChild获取菜单栏pMenuBar->setVisible(false);                              //隐藏菜单栏QList<QToolBar*> childeList = findChildren<QToolBar*>();  //通过findChildren获取工具栏childeList.at(0)->setVisible(false);                      //隐藏工具栏QStatusBar* pStatusBar = findChild<QStatusBar*>();        //获取状态栏pStatusBar->setVisible(false);                            //隐藏状态栏
}
http://www.lryc.cn/news/21940.html

相关文章:

  • MIT 6.S081学习笔记
  • 《网络安全入门到精通》 - 2.1 - Windows基础 - DOS命令Windows防火墙Windows共享文件
  • 八、Vben框架动态生成可编辑Table
  • 浅谈ERP数据的重要性
  • 【RabbitMQ笔记06】消息队列RabbitMQ七种模式之Topics主题模式
  • ChatGPT似乎有的时候并不能搞懂Java的动态分派,你懂了吗?
  • 【C++初阶】vector的模拟实现
  • 微信小程序、小游戏的流量主一般可以赚多少钱?
  • jni-Demo-基于linux(c++ java)
  • 指针的进阶——(1)
  • 电商平台的促销活动如何抵御大流量的ddos攻击
  • 代码随想录-48-104. 二叉树的最大深度
  • 【Vue3源码】第六章 computed的实现
  • Java基础之注解
  • 三、线性表
  • C++统计方形
  • Tina_Linux配网开发指南
  • 高频面试题|RabbitMQ如何防止消息的重复消费?
  • 黑盒测试用例设计方法-边界值分析法
  • 项目风险管理中不可忽视的5个关键点
  • Linux->进程地址空间
  • 【奶奶看了也不会】AI绘画 Mac安装stable-diffusion-webui绘制AI妹子保姆级教程
  • 基于stm32电梯管理系统设计
  • Spring中的FactoryBean 和 BeanFactory、BeanPostProcessor 和BeanFactoryPostProcessor解析
  • 【C++从入门到放弃】类和对象(上)
  • 什么牌子的蓝牙耳机便宜好用?四款高品质蓝牙耳机推荐
  • eddsa 算法
  • Xcode Developer Document 开发者文档
  • IntelliJ插件开发教程之新建项目
  • 解决SpringBoot中@RequestBody不能和Multipart同时传递的问题