3

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?

4
  • Have you tried setting a layout from Qt Designer? This way, resizing should be automatic (can't confirm for custom forms as I don't use them myself but I tend to use vertical layouts for plugins).
    – Joseph
    Commented Jan 26, 2018 at 15:06
  • Sorry for the delayed response; I didn't have as much time as I hoped for doing tests this week-end. Anyway, I gave it a try and unfortunately, setting a layout doesn't change anything...
    – Mefimefi
    Commented Jan 29, 2018 at 8:59
  • My mistake: setting a layout works indeed, but you have to be careful when you set the properties. It also seems to me that it doesn't work with all the layout types. I'll post a detailed answer below.
    – Mefimefi
    Commented Feb 1, 2018 at 13:12
  • Interesting, looking forward to your answer! Will have to keep this in mind if I ever create a UI form :)
    – Joseph
    Commented Feb 1, 2018 at 13:17

1 Answer 1

2

(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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.