How can I position QDockWidgets as the screen shot shows using code?

Nathan picture Nathan · Jun 9, 2010 · Viewed 8.6k times · Source

I want a Qt window to come up with the following arrangement of dock widgets on the right.

alt text http://img72.imageshack.us/img72/6180/docksonside.png

Qt allows you to provide an argument to the addDockWidget method of QMainWindow to specify the position (top, bottom, left or right) but apparently not how two QDockWidgets placed on the same side will be arranged.

Here is the code that adds the dock widgets. this uses PyQt4 but it should be the same for Qt with C++

self.memUseGraph = mem_use_widget(self)
self.memUseDock = QDockWidget("Memory Usage")
self.memUseDock.setObjectName("Memory Usage")
self.memUseDock.setWidget(self.memUseGraph)
self.addDockWidget(Qt.DockWidgetArea(Qt.RightDockWidgetArea),self.memUseDock)

self.diskUsageGraph = disk_usage_widget(self)
self.diskUsageDock = QDockWidget("Disk Usage")
self.diskUsageDock.setObjectName("Disk Usage")
self.diskUsageDock.setWidget(self.diskUsageGraph)
self.addDockWidget(Qt.DockWidgetArea(Qt.RightDockWidgetArea),self.diskUsageDock)

When this code is used to add both of them to the right side, one is above the other, not like the screen shot I made. The way I made that shot was to drag them there with the mouse after starting the program, but I need it to start that way.

Answer

mtvec picture mtvec · Jun 9, 2010

You can use QMainWindow::splitDockWidget .

From the docs:

Splits the space covered by the first dock widget into two parts, moves the first dock widget into the first part, and moves the second dock widget into the second part.

The orientation specifies how the space is divided: A Qt::Horizontal split places the second dock widget to the right of the first; a Qt::Vertical split places the second dock widget below the first.

You have to set QMainWindow::dockNestingEnabled to true first (but I guess you already did that).