Nominate our 2022 Qt Champions!

How to split QDockWidgets with equal size?

  • In my application I allow the user to split docks vertically or horizontally, using splitDockWidget(), so if the user splits the first dock and then splits again the two it can get 2x2 docks of equal size without having to adjust them.

    Horizontally, splits get equal width. However, when I split vertically, the second dock gets no height Oo

    I searched on the web and essentially found doc quotes recommending to set size policies in the widget inside the QDockWidget.
    But I have no idea how to tell "take half the space when splitting a dock" with these policies :(
    I tried to resize the content before docking, no effect at all.
    I also don't get why it works only horizontally...

    How can I do this?

  • Lifetime Qt Champion


    No height ?

    Can you provide a minimal code sample that shows that behavior ?

  • @SGaist By "no height", I mean it's tiny, or has a zero-height.
    I reproduced the behaviour with this code:


    class MainWindow : public QMainWindow
        explicit MainWindow(QWidget *parent = 0);
    private slots:
        void splitDockHorizontal();
        void splitDockVertical();
        QDockWidget * m_dock1;


    MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
        m_dock1 = new QDockWidget("Dock 1", this);
        addDockWidget(Qt::LeftDockWidgetArea, m_dock1);
        QWidget * dock1Content = new QWidget(this);
        QVBoxLayout * boxLayout = new QVBoxLayout();
        QPushButton * btnH = new QPushButton("SplitH", dock1Content);
        QPushButton * btnV = new QPushButton("SplitV", dock1Content);
        connect(btnH, SIGNAL(clicked()), this, SLOT(splitDockHorizontal()));
        connect(btnV, SIGNAL(clicked()), this, SLOT(splitDockVertical()));
    void MainWindow::splitDockHorizontal()
        QWidget * content = new QWidget(this);
        QDockWidget * dock = new QDockWidget("Dock +", this);
        splitDockWidget(m_dock1, dock, Qt::Horizontal);
    void MainWindow::splitDockVertical()
        QWidget * content = new QWidget(this);
        QDockWidget * dock = new QDockWidget("Dock +", this);
        splitDockWidget(m_dock1, dock, Qt::Vertical);

  • Lifetime Qt Champion

    That's because when you create your new widget they are empty so splitting horizontally you have the height of m_dock1 that also applied to your newly created dock however when splitting vertically only the width will be used.

  • @SGaist I know my widget is empty, however it should not prevent it from getting half the size. In my use case, it actually contains a native window (with no Qt widget inside), which has no preferred size. If I were to put it floating, maybe I could set it a size, but here I'm clueless. That's why I also tried to resize it before docking, but it doesnt works...

  • Lifetime Qt Champion

    In that case you should settle for a size you know is good for that window and set it on the widget

Log in to reply