views:

126

answers:

2

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

alt text

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.

A: 

I never tried it but I think you can set the orientation of the dock widget when adding it to the main window:

void QMainWindow::addDockWidget ( Qt::DockWidgetArea area, QDockWidget * dockwidget, Qt::Orientation orientation )

Stephen Chu
I really should have seen that in the docs.
Nathan
Actually, I read that in the docs but didn't realize what it was for until I saw your question. :)
Stephen Chu
+1  A: 

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).

Job
This would probably work too, but I have not tried it. I'll bet this is the code underlying the repositioning of docks using the mouse.
Nathan