When I have a QMainWindow with a grid layout, when resizing it with the mouse, it won't go below some minimum size that's necessary for all the controls in it to show properly. In my app I sometimes programatically hide controls, but then the window remains the same size and the rest of the controls look spread out, with too much space between them. I end up resizing the dialog manually so it doesn't look ugly.
Can I programatically set the dialog's vertical size to this minimum I get when manually resizing after I've hidden controls in it?
This code will do the trick (it resizes only the height):
I have found that resizing the
centralWidget
in theQMainWindow
then resizing theQMainWindow
itself does the trick. In other words:Note that there are other widgets in the QMainWindowLayout. Per this image from the Qt Documentation, there are also Dock Widgets and other stuff that could be present. I only use the
centralWidget
so this solution works for me.There is hierarchy between QLayout::SizeConstraint, QWidget::minimumSizeHint, QWidget::minimumSize, and you can find it in the documentation.
QWidget::minimumSize
is not set by default. When it is, it prevails overQWidget::minimumSizeHint
QWidget::minimumSizeHint
is invalid if the widget is not in a layout (meaning that it can be resized to 0 with the mouse), otherwise use the one defined by the layout.QLayout::SizeConstraint
holds the default layout behavior of the widgets it *directly * manage. If you nest a layoutA
within a layoutB
, all widgets added toA
will use its property. Also if a widgetW
inB
define its own layout, then this layout constraints are the one to be applied for the widgetW
.Knowing that, follow these steps. It might work, i didn't try :) :
QLayout::SetDefaultConstraint
.