Qt查找子窗口

在mfc中最通常的方法是CWnd* GetDlgItem( int nID ) const;  通过能ID获取子窗口;

每个资源都有一个nID作为唯一标识;


在Qt中可以通过 QObject::findChild(const QString & name = QString(), Qt::FindChildOptions options = Qt::FindChildrenRecursively) const;

T QObject::findChild(const QString & name = QString(), Qt::FindChildOptions options = Qt::FindChildrenRecursively) const
Returns the child of this object that can be cast into type T and that is called name, or 0 if there is no such object. Omitting the name argument causes all object names to be matched. The search is performed recursively, unless options specifies the option FindDirectChildrenOnly.


If there is more than one child matching the search, the most direct ancestor is returned. If there are several direct ancestors, it is undefined which one will be returned. In that case, findChildren() should be used.


This example returns a child QPushButton of parentWidget named "button1", even if the button isn't a direct child of the parent:


QPushButton *button = parentWidget->findChild<QPushButton *>("button1");
This example returns a QListWidget child of parentWidget:


QListWidget *list = parentWidget->findChild<QListWidget *>();
This example returns a child QPushButton of parentWidget (its direct parent) named "button1":


QPushButton *button = parentWidget->findChild<QPushButton *>("button1", Qt::FindDirectChildOnly);
This example returns a QListWidget child of parentWidget, its direct parent:


QListWidget *list = parentWidget->findChild<QListWidget *>(QString(), Qt::FindDirectChildOnly);

//

应用:

对于每个继承与QObject的类,都可以setObjectName(const QString & name)设置类的对象名称。

对于控件类,可以直接在“Qt 设计师”中直接获取,修改。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

chinabinlang

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

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

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

打赏作者

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

抵扣说明:

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

余额充值