11

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?

Nythagoras
  • 131
  • 1
  • 5
  • 3
    I did some research on this subject a couple of years ago and unfortunately QDockWidget seems to ignore most layout and size hints you provide. I ended up with a complex approach involving setMinimumWidth/Height and setMaximumWidth/Height as well. – Daniel Hedberg Jan 08 '13 at 17:59

3 Answers3

2

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.

Godkiller
  • 66
  • 3
  • 4
    Yeah, I already read this several times. And I tried for hours to achieve the correct resizing by implementing sizeHint() and setting the various policies but nothing had the wished effect. Until I see a working example, I won't believe that part of the documentation. Apart from that, I still want the DockWidges to restore their last size for not forcing the user to resize them each time he changes the shown DockWidget. – Nythagoras Jan 16 '13 at 13:35
  • @Nythagoras ...and 7 years later I have the same problem and the same desperation :-) – Oak_3260548 Aug 06 '20 at 18:33
1

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.

moodboom
  • 6,225
  • 2
  • 41
  • 45
1

I suggest to overload

protected :
virtual bool    event ( QEvent * event );

and catch the event which change your size

for example :

QRect mGeo;
bool    MyDockWidget::event ( QEvent * aEvent )
{
    if(aEvent->isAccepted ())
    {
        if(aEvent->type()==QEvent::Hide)
        {
            mGeo=this->geometry();
        }
        if(aEvent->type()==QEvent::Show)
        {
            this->setGeometry(mGeo);
        }
    }
    return QDockWidget::event(aEvent);
}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Zennoi
  • 13
  • 3
  • setGeometry doesn't seem to set the geometry to the requested QRect. Program behaves the same with and without setGeometry. – Nythagoras Nov 26 '16 at 22:25