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 设计师”中直接获取,修改。

你可能感兴趣的:(Qt查找子窗口)