How to make a Qt Widget grow with the window size?

Qt4AutoresizeQt Designer

Qt4 Problem Overview


I want to have a small QFormLayout that grows to fill its parent widget.

I created a new .ui file using the QWidget template in Qt Designer. I put a QFormLayout inside that 'window', then put some controls inside that QFormLayout.

This all works reasonably well, but the QFormLayout always stays at the size I set in Qt Designer. I would like the QFormLayout to fill its parent widget and grow/shrink with it.

How can I accomplish that?

Qt4 Solutions


Solution 1 - Qt4

In Designer, activate the centralWidget and assign a layout, e.g. horizontal or vertical layout. Then your QFormLayout will automatically resize.

Image of Designer

Always make sure, that all widgets have a layout! Otherwise, automatic resizing will break with that widget!

See also

> Controls insist on being too large, and won't resize, in QtDesigner

Solution 2 - Qt4

I found it was impossible to assign a layout to the centralwidget until I had added at least one child beneath it. Then I could highlight the tiny icon with the red 'disabled' mark and then click on a layout in the Designer toolbar at top.

Solution 3 - Qt4

The accepted answer (its image) is wrong, at least now in QT5. Instead you should assign a layout to the root object/widget (pointing to the aforementioned image, it should be the MainWindow instead of centralWidget). Also note that you must have at least one QObject created beneath it for this to work. Do this and your ui will become responsive to window resizing.

Solution 4 - Qt4

You need to change the default layout type of top level QWidget object from Break layout type to other layout types (Vertical Layout, Horizontal Layout, Grid Layout, Form Layout). For example: enter image description here

To something like this:

enter image description here

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbastibeView Question on Stackoverflow
Solution 1 - Qt4JensView Answer on Stackoverflow
Solution 2 - Qt4Jillian Beuschel MarohnicView Answer on Stackoverflow
Solution 3 - Qt4KeyC0deView Answer on Stackoverflow
Solution 4 - Qt4Linh DaoView Answer on Stackoverflow