Search code examples
qtqt5qdockwidget

Un-tabify three QDockWidgets programmatically


I have an application with three dock widgets. Those are created by the following code:

dock = new QDockWidget(tr("Some title"));
dock->setWidget(some_widget);
dock->setContextMenuPolicy(Qt::PreventContextMenu);
dock->setFeatures(QDockWidget::DockWidgetMovable | QDockWidget::DockWidgetFloatable);
addDockWidget(Qt::TopDockWidgetArea, dock);

dock2 and dock3 are then tabified by

tabifyDockWidget(dock1, dock2);
tabifyDockWidget(dock2, dock3);

The window then looks like this: Tabbed docks

I can arrange the docks side by side by dragging and dropping them, so that the window looks like this: side-by-side docks

I'd like to do this programmatically, but I can't figure out how. When doing a

splitDockWidget(dock1, dock2, Qt::Horizontal);

nothing happens. When doing

splitDockWidget(dock1, dock2, Qt::Vertical);

dock1 and dock2 disappear, and only dock3 is still visible: dock1 and dock2 invisible

After manually dragging it out of the main window and back in, the window looks like this: wrongly arranged docks

So what am I doing wrong here?


Solution

  • I found the solution. The problem is to which dockarea the docks are attached to. I added

    setDockNestingEnabled(true);
    

    and the following fuctions, which do the job by first changing the dockarea if needed:

    void MainWindow::attachDocks(Qt::DockWidgetArea area)
    {
        m_playersDock->setFloating(false);
        m_scoreDock->setFloating(false);
        m_rankingDock->setFloating(false);
        addDockWidget(area, m_playersDock);
        addDockWidget(area, m_scoreDock);
        addDockWidget(area, m_rankingDock);
    }
    
    void MainWindow::arrangeTabified()
    {
        attachDocks(Qt::TopDockWidgetArea);
        tabifyDockWidget(m_playersDock, m_scoreDock);
        tabifyDockWidget(m_scoreDock, m_rankingDock);
    }
    
    void MainWindow::arrangeHorizontally()
    {
        attachDocks(Qt::TopDockWidgetArea);
        splitDockWidget(m_playersDock, m_scoreDock, Qt::Horizontal);
        splitDockWidget(m_scoreDock, m_rankingDock, Qt::Horizontal);
    }
    
    void MainWindow::arrangeVertically()
    {
        attachDocks(Qt::LeftDockWidgetArea);
        splitDockWidget(m_playersDock, m_scoreDock, Qt::Vertical);
        splitDockWidget(m_scoreDock, m_rankingDock, Qt::Vertical);
    }
    
    void MainWindow::arrangeOwnWindow()
    {
        m_playersDock->setFloating(true);
        m_scoreDock->setFloating(true);
        m_rankingDock->setFloating(true);
    }