Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force Tabbed Dock in QMainWindow Qt5.7

Tags:

c++

qt

I'm trying to get my QMainWindow to allow only tabbed QDockWidgets. If I understood the Qt Documentation right it should work with the setDockOptions-method.

The following code didn't work for me:

 QMainWindow window;
 window.setDockOptions(QMainWindow::ForceTabbedDocks);

What am I doing wrong? Or is it a bug in the current Qt version? I'm coding on a MacPro an I'm using Qt 5.7.

thanks

like image 282
Janis Avatar asked Jan 15 '17 17:01

Janis


1 Answers

ForceTabbedDocks only applies to user interactions with the docks.

To programatically add new docks in tabs, you need to use QMainWindow::tabifyDockWidgets. For example,

void MainWindow::addTabbedDock(Qt::DockWidgetArea area, QDockWidget *widget)
{
    QList<QDockWidget*> allDockWidgets = findChildren<QDockWidget*>();
    QVector<QDockWidget*> areaDockWidgets;
    for(QDockWidget *w : allDockWidgets) {
        if(dockWidgetArea(w) == area) {
            areaDockWidgets.append(w);
        }
    }

    if(areaDockWidgets.empty()) {
        // no other widgets
        addDockWidget(area, widget);
    } else {
        tabifyDockWidget(areaDockWidgets.last(), widget);
    }
}
like image 112
Xian Nox Avatar answered Oct 23 '22 19:10

Xian Nox