Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 510
Nathan Avatar asked Dec 10 '22 15:12

Nathan


2 Answers

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

like image 155
mtvec Avatar answered Feb 27 '23 11:02

mtvec


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 )

like image 38
Stephen Chu Avatar answered Feb 27 '23 13:02

Stephen Chu