Search code examples
qtqwidget

How to show child QWidget titlebar when it's in parent QWidget layout?


I have parent widget and two child widgets in it (most likely I will put them in QSplitter). I want to display their names to clearly define where stuff is. Preferably in a frame at the top of the widget, like it's an embedded window.

How to do that in Qt?

Example of what I want to achieve: enter image description here


Solution

  • Here's a way to achieve that:

    • Use a QMainWindow.
    • Set its central widget to a QSplitter.
    • Add to QMainWindows to the splitter.
    • Add 2 QToolBars to the sub main windows:
      • Top one for the title.
      • Bottom one as the actual tool bar.

    Example:

    #include <QApplication>
    #include <QtWidgets>
    
    int main(int argc,char*argv[])
    {
        QApplication a(argc, argv);
    
        QMainWindow ww;
    
        ww.addToolBar("Main ToolBar");
    
        QSplitter *s = new QSplitter(Qt::Horizontal,&ww);
        ww.setCentralWidget(s);
    
        QMainWindow *left = new QMainWindow(&ww);
        QToolBar *t = new QToolBar("Left Toolbar");
        t->setMovable(false);
        t->addWidget(new QLabel("ChildWidget 1"));
        left->addToolBar(t);
        left->addToolBarBreak();
        left->addToolBar("Left tool bar");
    
        QMainWindow *right = new QMainWindow(&ww);
        QToolBar *t1 = new QToolBar("Right Toolbar");
        t1->setMovable(false);
        t1->addWidget(new QLabel("ChildWidget 2"));
        right->addToolBar(t1);
        right->addToolBarBreak();
        right->addToolBar("Right tool bar");
    
        s->addWidget(left);
        s->addWidget(right);
    
        ww.show();
    
        return a.exec();
    }
    

    Result:

    2 mainwindows in a mainwindow