I have a QMainWindow
that contains several QDockWidget
s. Only one of them should be shown at a time. My Problem is:
When I hide a dockWidget and show another, the size of the newly shown is the same as the just hidden, no matter what QSizePolicys, sizeHint, sizeConstraint I set! I want the newly shown to restore its own last size but I can't find any method to resize a QDockWidget
, without fixing its size with setMinimumSize
and setMaximumSize
.
In fact there is one way but I consider it very ugly:
setMinimumWidth(500);
setMaximumWidth(500);
qApp().processEvents();
setMinimumWidth(0);
setMaximumWidth(9999);
There must be a better way?! Any suggestions?
From the documentation:
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.
Which means that instead of resizing the DockWidget, you should be resizing the child widget.
I tried the solution you suggested in your question and it works for me, although there is an ugly flash while the widget goes through an extra paint cycle. I haven't found a better way, so I'll use it for now until Qt releases better support for QDockWidget.
I'm hopeful that there will be more functionality added to the QDockWidget API. It's a great API, but there are several areas that are still sorely lacking. For example, this suggested method of obtaining the index of a tabbed QDockWidget (right from the Qt FAQ) is cumbersome and error prone.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With