Results 1 to 3 of 3

Thread: QObject::findChildren

  1. #1
    Join Date
    Jan 2006
    Location
    Munich, Germany.
    Posts
    111
    Thanks
    29
    Thanked 3 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default QObject::findChildren

    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
    Qt Code:
    1. QList<T> QObject::findChildren ( const QString & name = QString() ) const
    To copy to clipboard, switch view to plain text mode 
    or recursivly using
    Qt Code:
    1. T QObject::findChild ( const QString & name = QString() ) const
    To copy to clipboard, switch view to plain text mode 

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

    thanks
    K

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QObject::findChildren

    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.

  3. #3
    Join Date
    Jan 2006
    Location
    Munich, Germany.
    Posts
    111
    Thanks
    29
    Thanked 3 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: QObject::findChildren

    ahh, yes, got it. (sorry)
    I was having some trouble with the syntax.

    For the MSVC people:
    Qt Code:
    1. QList<T> qFindChildren ( const QObject * obj, const QString & name )
    2. QList<QPushButton*> btnList = qFindChildren<QPushButton*> ( this, "" );
    To copy to clipboard, switch view to plain text mode 
    works perfectly.
    and there's also
    Qt Code:
    1. QList<QPushButton*> btnList = qFindChildren<QPushButton*> ( this );
    To copy to clipboard, switch view to plain text mode 


    much appreciated
    K

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.