Results 1 to 3 of 3

Thread: findChildren() via parentWidget()-> gives other output than via this-> in the parent

  1. #1
    Join Date
    Nov 2011
    Posts
    26
    Thanks
    6
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded Qt Jambi
    Platforms
    Unix/X11 Windows

    Default findChildren() via parentWidget()-> gives other output than via this-> in the parent

    Hey folks!

    It seems findChildren() does not find one specific widget (ever the same) if searched for via "parentWidget()->parentWidget()->findChildren<QWidget *>();"
    but it can be find if i do "findChildren<QWidget *>();" in parentWidget of the parentWidget() ... i am sry if it might sound confusing

    Code should say more than tousand words i think...
    There are 3 widgets (DeviceWrapperView, DeviceView, DPTWidget): DeviceViewWrapper contains DeviceView and DeviceView contains DPTWidget..

    Qt Code:
    1. void DPTWidget::disableWidgets(QList<QWidget*> except){
    2. qDebug() << "Search started ON: " << parentWidget()->parentWidget()->objectName();
    3. listOfWidgets = parentWidget()->parentWidget()->findChildren<QWidget *>();
    4. for (int i = 0; i < listOfWidgets.size(); i++) {
    5. qDebug() << "Objectname: " << listOfWidgets.at(i)->objectName();
    6. if(!except.contains(listOfWidgets.at(i))){
    7. if(listOfWidgets.at(i)->isEnabled() == false)
    8. listOfWidgets.removeAt(i);
    9. else
    10. listOfWidgets.at(i)->setEnabled(false);
    11. }
    12. else
    13. listOfWidgets.removeAt(i);
    14. }
    15. }
    To copy to clipboard, switch view to plain text mode 

    Output is:
    Search started ON: "deviceViewWrapperStack"
    Objectname: "gotoHome"
    Objectname: "floorsComboBox"
    Objectname: ""
    Objectname: "qt_scrollarea_viewport"
    Objectname: ""
    Objectname: ""
    Objectname: ""
    Objectname: ""
    Objectname: "qt_scrollarea_hcontainer"
    Objectname: "qt_scrollarea_vcontainer"
    Objectname: "deviceView"
    Objectname: "deviceTableView"
    Objectname: ""
    Objectname: "qt_scrollarea_hcontainer"
    Objectname: "qt_scrollarea_vcontainer" .......


    Code in DeviceViewWrapper
    Qt Code:
    1. void DeviceViewWrapper::setViewMode(QString viewMode){
    2. qDebug() << "ViewMode: " << viewMode;
    3. ui.deviceView->setViewMode(viewMode);
    4.  
    5. QList<QWidget *> listOfWidgets = this->findChildren<QWidget *>();
    6. for (int i = 0; i < listOfWidgets.size(); i++) {
    7. qDebug() << "Objectname: " << listOfWidgets.at(i)->objectName();
    8. }
    9. qDebug() << "MY Objectname: " << this->objectName();
    10. }
    To copy to clipboard, switch view to plain text mode 

    Output is:
    Objectname: "gotoHome"
    Objectname: "floorsComboBox"
    Objectname: ""
    Objectname: ""
    Objectname: "qt_scrollarea_viewport"
    Objectname: "qt_scrollarea_hcontainer"
    Objectname: ""
    Objectname: "qt_scrollarea_vcontainer"
    Objectname: ""
    Objectname: "roomsComboBox"
    Objectname: ""
    Objectname: ""
    Objectname: "qt_scrollarea_viewport"
    Objectname: "qt_scrollarea_hcontainer"
    Objectname: ""
    Objectname: "qt_scrollarea_vcontainer"
    Objectname: ""
    Objectname: "deviceView"
    Objectname: "deviceTableView" .......
    .......................
    MY Objectname: "deviceViewWrapperStack"


    I am in fact missing the "roomsComboBox" in the output of "DPTWidget", according to the output of objectName() the 2 Objects where the searches were executed to are identical ..?

    Hopefully somebody has an idea what i did wrong, maybe i am blind ...
    Thanks!

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: findChildren() via parentWidget()-> gives other output than via this-> in the par

    It is not a good idea to modify the list in the for lloop

    Qt Code:
    1. void DPTWidget::disableWidgets(QList<QWidget*> except){
    2. qDebug() << "Search started ON: " << parentWidget()->parentWidget()->objectName();
    3. listOfWidgets = parentWidget()->parentWidget()->findChildren<QWidget *>();
    4. for (int i = 0; i < listOfWidgets.size(); i++) {
    5. qDebug() << "Objectname: " << listOfWidgets.at(i)->objectName();
    6. if(!except.contains(listOfWidgets.at(i))){
    7. if(listOfWidgets.at(i)->isEnabled() == false)
    8. listOfWidgets.removeAt(i); //<<<<<<<<<<<<<<<<<<<<<< listOfWidgets.size() is modified here, also you will miss the i'th item in the list
    9. else
    10. listOfWidgets.at(i)->setEnabled(false);
    11. }
    12. else
    13. listOfWidgets.removeAt(i); //<<<<<<<<<<<<<<<<<<<<<< listOfWidgets.size() is modified here, also you will miss the i'th item in the list
    14. }
    15. }
    To copy to clipboard, switch view to plain text mode 

    Try the following

    Qt Code:
    1. void DPTWidget::disableWidgets(QList<QWidget*> except){
    2. qDebug() << "Search started ON: " << parentWidget()->parentWidget()->objectName();
    3. listOfWidgets = parentWidget()->parentWidget()->findChildren<QWidget *>();
    4. int i = 0;
    5. while(i < listOfWidgets.size()) {
    6. qDebug() << "Objectname: " << listOfWidgets.at(i)->objectName();
    7. if(!except.contains(listOfWidgets.at(i))){
    8. if(listOfWidgets.at(i)->isEnabled() == false)
    9. {
    10. listOfWidgets.removeAt(i);
    11. continue;
    12. }
    13. else
    14. listOfWidgets.at(i)->setEnabled(false);
    15. }
    16. else
    17. {
    18. listOfWidgets.removeAt(i);
    19. continue;
    20. }
    21. i++;
    22. }
    23. }
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

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

    shock (28th November 2011)

  4. #3
    Join Date
    Nov 2011
    Posts
    26
    Thanks
    6
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded Qt Jambi
    Platforms
    Unix/X11 Windows

    Default Re: findChildren() via parentWidget()-> gives other output than via this-> in the par

    Ahmm... in fact... you're right i have not thought of that .. my fault
    Thanks works as desired

Similar Threads

  1. Replies: 3
    Last Post: 27th August 2011, 21:44
  2. Replies: 5
    Last Post: 21st April 2010, 21:36
  3. findChildren<QwtPlot *>()
    By gib in forum Qwt
    Replies: 0
    Last Post: 23rd March 2010, 03:09
  4. QProcess : child process listening parent output ?
    By Nyphel in forum Qt Programming
    Replies: 16
    Last Post: 20th March 2007, 13:40
  5. QObject::findChildren
    By TheKedge in forum Qt Programming
    Replies: 2
    Last Post: 2nd March 2006, 10:34

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.