Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QTabWidget not appearing when findChildren is called.



  • Hello!

    I am trying to find all of the children of a layout. One of these children is a QTabWidget. It does not appear when called. Code is as follows:

     MainWindow::buildFunction()
     {
          configTabArea = new QTabWidget;
          configMainVbox = new QVBoxLayout;
          myHbox = new QHBoxLayout;
          button = new QPushButton;
          ...work...
          myHbox->addWidget(button);
          configMainVbox->addWidget(configTabArea);
          configMainVbox->addLayout(myHbox);
     }
    
     MainWindow::actionFunction()
     {
          QList<QLayout*> layoutList = configMainVbox->findChildren<QLayout *>();
          // line above finds one layout, the QHBoxLayout
    
          QList<QTabWidget*> tabList = configMainVbox->findChildren<QTabWidget *>():
          //this line finds nothing. Same when using QWidget instead of QTabWidget
     }
    

    Any help would be greatly appreciated



  • Try

    MainWindow::buildFunction()
     {
          configTabArea = new QTabWidget(this);
    configTabArea->setObjectName("configTabArea"); //Set the name
          configMainVbox = new QVBoxLayout;
          myHbox = new QHBoxLayout;
          button = new QPushButton;
          ...work...
          myHbox->addWidget(button);
          configMainVbox->addWidget(configTabArea);
          configMainVbox->addLayout(myHbox);
     }
    
     MainWindow::actionFunction()
     {
          QList<QLayout*> layoutList = configMainVbox->findChildren<QLayout *>();
          // line above finds one layout, the QHBoxLayout
    
          QTabWidget* tabList = findChild<QTabWidget *>("configTabArea"):
          //Check that tabList is not null
     }
    


  • Thank you for your reply!

    So I tried your suggestion and it returns a null QObject.

    I noticed you set the parent of QTabWidget to this. This is all taking place in a non-modal dialog configScreen so I tried explicitly setting that as the parent but it didn't seem to have any effect.


  • Lifetime Qt Champion

    @mar0029 said:

    • This is all taking place in a non-modal dialog configScreen

    But you seems to try to find the QTabWidget* in the context of MainWindow ?
    If QTabWidget lives in dialog ,
    you cannot find it from context of mainwindow unless
    its dialog->findChild



  • Thank you for your response!

    One correction on my part, everything takes place within a modal dialog and not a non-modal one.

    I think I may have miss spoke. I'm posting a more complete version of my code below, minus details on what happens in the window.

     void MainWindow::configApply()
     {
         QList<QLayout*> layoutList = configMainVbox->findChildren<QLayout*>();
    
    qDebug() << "layoutList size " << layoutList.size();
    
    QTabWidget* tabWidget = configScreen->layout()->layout()->findChild<QTabWidget*>("configTabArea");
    
    if(tabWidget != NULL)
    {
        qDebug() << "is OK ";
    }
    else
        qDebug() << "NULL";
     }
    
     void MainWindow::configFunction()
     {
    configScreen = new QDialog;
    configScreen->setFixedSize(575,500);
    
    myHbox = new QHBoxLayout;
    
    tab0 = new QWidget;
    tab1 = new QWidget;
    tab2 = new QWidget;
    tab3 = new QWidget;
    
    sa0 = new QScrollArea;
    sa1 = new QScrollArea;
    sa2 = new QScrollArea;
    sa3 = new QScrollArea;
    
    vbox0 = new QVBoxLayout;
    vbox1 = new QVBoxLayout;
    vbox2 = new QVBoxLayout;
    vbox3 = new QVBoxLayout;
    
    configTabArea = new QTabWidget;
    configMainVbox = new QVBoxLayout;
    configTabArea->setObjectName("configTabArea");
     //
     //stuff is added to the QVBoxLayouts
     //
       vbox0->setSizeConstraint(QLayout::SetFixedSize); //important to keep scrolling active
    vbox1->setSizeConstraint(QLayout::SetFixedSize);
    vbox2->setSizeConstraint(QLayout::SetFixedSize);
    vbox3->setSizeConstraint(QLayout::SetFixedSize);
    
    tab0->setLayout(vbox0);
    sa0->setWidget(tab0);
    
    tab1->setLayout(vbox1);
    sa1->setWidget(tab1);
    
    tab2->setLayout(vbox2);
    sa2->setWidget(tab2);
    
    tab3->setLayout(vbox3);
    sa3->setWidget(tab3);
    
    QPushButton *configApplyButton = new QPushButton("Apply");
    QPushButton *configCancelButton = new QPushButton("Cancel");
    
    myHbox->addWidget(configApplyButton);
    myHbox->addWidget(configCancelButton);
    
    configTabArea->addTab(sa0, tr("file1"));
    configTabArea->addTab(sa1, tr("file2"));
    configTabArea->addTab(sa2, tr("file3"));
    configTabArea->addTab(sa3, tr("file4"));
    
    configMainVbox->addWidget(configTabArea);
    configMainVbox->addLayout(myHbox);
    
    connect(configApplyButton, SIGNAL(clicked(bool)), this, SLOT(configApply()));
    connect(configCancelButton, SIGNAL(clicked(bool)), configScreen, SLOT(accept()));
    
    configScreen->setLayout(configMainVbox);
    configScreen->setWindowTitle("Set your file's configurations");
    
    configScreen->setAttribute(Qt::WA_DeleteOnClose);
    configScreen->exec();
    }
    

    let me know what you think.


  • Lifetime Qt Champion

    @mar0029 said:

    Hi
    Normally this is very easy so I wonder if something odd is going on :)

    can u try

    QList<QTabWidget*> AlltabWidgetslist = configScreen->findChildren<QTabWidget*>();

    qDebug << "num of tabs:" << AlltabWidgetslist.count();

    and see if it say 1



  • @mrjj
    Thank you very much. This started me on the right path.


Log in to reply