Results 1 to 4 of 4

Thread: QObject::findChildren() strange behavior

  1. #1
    Join Date
    Nov 2011
    Posts
    79
    Thanks
    5
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question QObject::findChildren() strange behavior

    In my program I have QDialog to edit configuration options and it is empty as program start.
    I use QPlugin modules so each module can add its own panel to this dialog. Base class of such panel is:
    Qt Code:
    1. class ConfigWidget : public QWidget
    2. {
    3. public:
    4. explicit ConfigWidget(QWidget *parent = 0) : QWidget(parent){}
    5. virtual void SetValues(){}
    6. virtual void SaveValues(){}
    7. };
    To copy to clipboard, switch view to plain text mode 
    And the code of accept() signal of this dialog:
    Qt Code:
    1. void ConfigDialog::okClicked()
    2. {
    3. QList<ConfigWidget *> widgets = findChildren<ConfigWidget *>();
    4. foreach(ConfigWidget * widget,widgets)
    5. {
    6. widget->SaveValues();
    7. }
    8. Config::instance()->SaveConfig();
    9. accept();
    10. }
    To copy to clipboard, switch view to plain text mode 
    I just get all the panels and save its values. I guess it is very simple and clear.
    But findChildren returns all the widgets of dialog - buttons, lineedits etc. And it is very very strange for me.
    From docs: Returns all children of this object with the given name that can be cast to type T
    How QPushButton can be cast to ConfigWidget?? QPushButton have no SaveValues() function so I am very disappointed here ...
    And, if it is expected result of findChildren(), how can I get only widgets derived from ConfigWidget?

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QObject::findChildren() strange behavior

    Add the Q_OBJECT macro to your ConfigDialog class and rerun qmake to ensure that it gets run through moc. Without this macro there is no QObject runtime data for this class, so you get the underlying QWidget data. All widgets can be cast to QWidget.

  3. The following user says thank you to ChrisW67 for this useful post:

    folibis (2nd December 2013)

  4. #3
    Join Date
    Nov 2011
    Posts
    79
    Thanks
    5
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QObject::findChildren() strange behavior

    Thanks, ChrisW67, it works!

  5. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QObject::findChildren() strange behavior

    If your plugins can create ConfigWidget* you might have access to them at creation time at some point. In that case storing them in a QList<ConfigWidget*> would be more efficient, since you don't have to traverse the whole widget tree, nor need any casting.

    Cheers,
    _

Similar Threads

  1. setCellWidget - strange behavior
    By Archa4 in forum Newbie
    Replies: 5
    Last Post: 28th April 2011, 08:26
  2. QSqlDatabase strange behavior
    By macav in forum Qt Programming
    Replies: 1
    Last Post: 9th February 2011, 15:21
  3. Qt Creator Strange behavior of GDB in Creator 1.3
    By rayjc in forum Qt Tools
    Replies: 1
    Last Post: 11th March 2010, 21:42
  4. Strange behavior of QSyntaxHighlighter in Qt3
    By lorebett in forum Qt Programming
    Replies: 1
    Last Post: 18th January 2009, 14:50
  5. QObject::findChildren
    By TheKedge in forum Qt Programming
    Replies: 2
    Last Post: 2nd March 2006, 10:34

Tags for this Thread

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.