Search code examples
qtqobjectqdockwidget

Qt findChildren() function only return first child object


I met a trouble with QDockWidget and QMainWindow, I add three dockwidgets in mainwindow and tabify them. In order to customize these tabbars, using QMainWindow::findChildren() function to get the pointers of tabbars. But return result gives me only the first tabbar pointer in the list.

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    QWidget* widCenter =this->takeCentralWidget();
    if(widCenter)
    {
        delete widCenter;
        widCenter = nullptr;
    }
    MyDockWidget* m1 = new MyDockWidget(this);
    MyDockWidget* m2 = new MyDockWidget(this);
    MyDockWidget* m3 = new MyDockWidget(this);
    m1->setWindowTitle("m1");
    m2->setWindowTitle("m2");
    m3->setWindowTitle("m3");

    this->setDockOptions(QMainWindow::AllowTabbedDocks);
    this->addDockWidget(Qt::TopDockWidgetArea, m1);
    this->addDockWidget(Qt::TopDockWidgetArea, m2);
    this->addDockWidget(Qt::TopDockWidgetArea, m3);

    //this->splitDockWidget(m1, m2, Qt::Horizontal);
    this->tabifyDockWidget(m1, m2);
    this->tabifyDockWidget(m2, m3);
    this->setTabPosition(Qt::TopDockWidgetArea, QTabWidget::North);

     m_list=this->findChildren<QTabBar*>(QString(), Qt::FindChildrenRecursively);
     qDebug() << "size = "<<m_list.size();
     if(m_list.size() > 0)
     {
         qDebug() << "text = "<< m_list[0]->tabText(0);
         qDebug() << "text = "<< m_list[0]->tabText(1);
         qDebug() << "text = "<< m_list[0]->tabText(2);
     }
}

and debug result:

size = 1
text = "m1"

Solution

  • You do have only one tabbar from the little code you have provided. QObject::findChildren() has its options parameter set to Qt::FindChildrenRecursively by default so if you've had more, it would have counted more.

    In general you have the following situation:

    • tabbar contains tabs
    • each tab represents a docked widget