Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

resize a QDockWidget

I have a QMainWindow that contains several QDockWidgets. 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?

like image 850
Nythagoras Avatar asked Jan 08 '13 13:01

Nythagoras


2 Answers

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.

like image 69
Godkiller Avatar answered Oct 20 '22 04:10

Godkiller


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.

like image 35
moodboom Avatar answered Oct 20 '22 03:10

moodboom