Reputation: 11768
I have a PyQt 4 application that has a QMainWindow with docked QDockWidgets in the left and bottom dock areas. I currently have two widgets docked on the left and 2 on the bottom. I'm trying to figure out how I can programmatically move the boundary between the central widget and the dock areas, effectively changing the dock area's width (in the case of the left or right areas) or height (for the top and bottom dock areas).
I can do this manually with the mouse; when I move the cursor over the boundary between the central widget and the dock areas, I get a resize handle that I can use to stretch the dock area. I have yet to find an interface to allow me to do that from my program. I've tried manually resizing the QDockWidget objects themselves (which the documentation recommends against) and the widgets that they wrap (which should work), but that doesn't seem to work. Any ideas?
Specifically, this sort of approach does not work:
dock1.resize(QSize(width, height))
dock2.resize(QSize(width, height))
dock3.resize(QSize(width, height))
Nor does this:
dock1.widget().resize(QSize(width, height))
dock2.widget().resize(QSize(width, height))
dock3.widget().resize(QSize(width, height))
Upvotes: 1
Views: 6975
Reputation: 36745
I think I understand what happens. First a couple of notes from the docs.
First QMainWindow
:
QMainWindow has its own layout to which you can add QToolBars, QDockWidgets, a QMenuBar, and a QStatusBar. The layout has a center area that can be occupied by any kind of widget.
A QMainWindow
has its own layout manager, which means it will manage the layout of its items. I couldn't find any reference in docs but I guess the layout manager tries to maximize the area for centralWidget
. Therefore all other elements are shrunk to their bare minimum.
Lets look at QDockWidget
:
A QDockWidget acts as a wrapper for its child widget, set with setWidget(). Custom size hints, minimum and maximum sizes and size policies should be implemented in the child widget. QDockWidget will respect them, adjusting its own constraints to include the frame and title. Size constraints should not be set on the QDockWidget itself, because they change depending on whether it is docked; a docked QDockWidget has no frame and a smaller title bar.
So this means you should alter the size of the widget inside the QDockWidget
. When a QDockWidget
is floating it will be free to resize as its widget demands. But docking it will leave that to the QMainWindow
s layout manager and it will shrink.
Probably the easiest solution is setting the minimumSize
for the widget inside QDockWidget
so that the layout manager in QMainWindow
will not shrink it beyond that. Alternatively you could subclass and re-implement sizeHint
method for your widgets so that the layout manager will get a preferred size.
In short, this should work:
dock1.widget().setMinimumSize(QSize(width, height))
dock2.widget().setMinimumSize(QSize(width, height))
dock3.widget().setMinimumSize(QSize(width, height))
Upvotes: 8