View Full Version : QObject::findChildren

2nd March 2006, 10:21
Hello all,

I'd like to get a list of all (say) QPushButtons in my widget. I can get a list off all child ofjects using

QList<T> QObject::findChildren ( const QString & name = QString() ) const
or recursivly using

T QObject::findChild ( const QString & name = QString() ) const

how do I then test my <T>s to see if they are a specific type, i.e. QPushButtons?


2nd March 2006, 10:38
Just as it says in the docs:

QList<T> QObject::findChildren ( const QString & name = QString() ) const

Returns all children of this object with the given name that can be cast to type T, or an empty list if there are no such objects. An empty string matches all object names. The search is performed recursively.

The following example shows how to find a list of child QWidgets of the specified parentWidget named widgetname:

QList<QWidget *> widgets = parentWidget.findChildren<QWidget *>("widgetname");

This example returns all QPushButtons that are children of parentWidget:

QList<QPushButton *> allPButtons = parentWidget.findChildren<QPushButton *>();

Warning: This function is not available with MSVC 6. Use qFindChildren() instead if you need to support that version of the compiler.

2nd March 2006, 11:34
ahh, yes, got it. (sorry)
I was having some trouble with the syntax.

For the MSVC people:

QList<T> qFindChildren ( const QObject * obj, const QString & name )
QList<QPushButton*> btnList = qFindChildren<QPushButton*> ( this, "" );
works perfectly.
and there's also

QList<QPushButton*> btnList = qFindChildren<QPushButton*> ( this );

much appreciated