[GIS] Autosize custom form window to its content

formspyqgisqgis-2.18qt-designer

I've created several custom forms using Qt Designer. My problem is that when I open a form, its window size remains the same as the size of the window of the last form I opened. For example, let's say I open the form below and manually adjust its window size so that it fits its contents:
enter image description here

Then, when I open a bigger form, it will look like this:

enter image description here

So, if I want to see the "OK" button, I have to adjust the window size manually again. This gets tedious in the long run.
I tried adding

<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
</sizepolicy>

at the beginning of the .ui files (before the "windowTitle" property), but it causes QGIS to crash when I try and open the forms. I also tried adding these lines:

dialog.parent().setFixedWidth(400)
dialog.parent().setFixedHeight(400)
dialog.parent().setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)

at the beginning of the function that is called when the form is opened, thinking my issue was similar to this one, but it doesn't make any difference.
So, how can I make the window autoresize to the contents of the custom form within?

Best Answer

(Thanks to @Joseph for pointing me in the right direction)

Here are the steps that worked for me:

  • Add a grid layout to your window (I found it easier to achieve what I wanted with this layout type, but I'm not used to layouts so feel free to try the others)
  • Resize the layout so it fits your window
  • In the layout properties, select "SetMinMaxSize" for the "layoutSizeConstraint" property
  • In the object inspector, right click on your dialog and in "size constraint" choose "define minimum size", then do it again and choose "define maximum size"

This way, the windows'size is never smaller than its content once you're in QGIS.

Related Question