问题
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?
回答1:
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.
回答2:
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.
回答3:
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);
}
来源:https://stackoverflow.com/questions/14215897/resize-a-qdockwidget