106

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?

Angie Quijano
  • 4,167
  • 3
  • 25
  • 30
bastibe
  • 16,551
  • 28
  • 95
  • 126

4 Answers4

153

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

Community
  • 1
  • 1
Jens
  • 6,173
  • 2
  • 24
  • 43
  • 21
    QtDesigner can be a bit confusing at times. – bastibe May 19 '11 at 06:54
  • 37
    oh, THANKS for that! :-) i've been staring at QtDesigner (well, QtCreator, same thing there though) for half an hour and could not figure out why my central widget doesn't resize to the max - until I spotted that the tiny little icon you have circled in your answer has a red 'disabled' mark over it. one click on the 'lay out vertically' button in the creator toolbar and the problem is fixed. First time I notice that toolbar BTW, easy to overlook... – ssc Feb 03 '12 at 04:06
  • 9
    To comment on Jens answer: On the mac, click on the top-level widget (not necessarily named "centralWidget") then click on "Form" in the menu bar and select one of the layouts. – hnasarat Aug 08 '12 at 16:04
  • 5
    If anyone's still wondering, this is done on Windows in QT5.8 /QT Creator 4.2.1 by clicking on the top level "centralWidget" (which has a little red mark on it) and then going to Tools>Form Editor>Lay Out Vertically (or horizontally). – LeoR Apr 28 '17 at 10:58
47

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.

  • 7
    **This is maddening.** Assigning a layout to the central widget *should* be the most trivial operation in Qt Creator. Instead, doing so requires combining this highly unintuitive solution with [Jens](https://stackoverflow.com/users/304090/jens)' [equally unintuitive solution](https://stackoverflow.com/a/6045152/2809027) (*in that exact order*). While the automation provided by Qt Creator still beats out repetitious boilerplate by a wide margin, hacking around its appalling user experience (UX) has given me a new eye-twitch. – Cecil Curry Jun 15 '17 at 06:08
  • @Cecil Curry even after that solution it took me 20 minutes to realize what i should ... because centralWidget in only case when there is no context menu option to add layout... only a micriscopic button in toolbar that looks almost like inactive one – Swift - Friday Pie Jul 26 '17 at 11:13
  • 4
    Writing this comment since even after the solutions here, I had trouble to find the option. You have to select the centralWidget, then right-click in some free space of your app, go to 'Layout' in the context-menu that opened and then select the layout you want. This worked for me, at least (QT5, using QT Designer 5.11.1). – kushy Aug 28 '18 at 12:19
12

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.

KeyC0de
  • 4,728
  • 8
  • 44
  • 68
  • There are 2 inaccuracies. 1. The screenshot of the accepted answer clearly shows a QMainWindow, which can **not** have a layout set (it has its own *internal* layout that, other than the central widget, also manages the menu bar, the status bar, and any possible tool bar or dock widget); in fact, any attempt to set a layout in Designer for the main window will actually result in setting it for the central widget. 2. The term "QObject" is wrong: QWidget (and related classes) *inherits* from QObject, which, in its own, is not a UI element. – musicamante Aug 06 '23 at 17:41
9

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) by pressing one of the layout related toolbar buttons. For example: enter image description here

To something like this:

enter image description here

Volfango
  • 3
  • 3
Linh Dao
  • 1,305
  • 1
  • 19
  • 31
  • 1
    Finally figured it out, and it took WAY too long, too. Refer to the first of Linh Dao's images. With the "centralWidget" selected, look at the toolbar above the graphical layout area. There are several layout related icons there. THIS is where you click to change the layout for the centralWidget (it's not in any popup menu). You will see the change reflected on the far right, in the icon of the "centralWidget" in the layout tree structure. NOW the layout properly resizes, stretching and shrinking elements as the window is resized. – Udo Schuermann Oct 17 '21 at 19:35