您的位置:首页 > 编程语言 > Qt开发

Qt查找子窗口

2013-11-27 12:25 281 查看
在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 设计师”中直接获取,修改。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: