236

I'm using the Qt Designer.

I want to create a QVBoxLayout which will automatically expand to fill the whole window.

The layout of the QVBoxLayout remains fixed.

How can I cause the QVBoxLayout to expand and fill the entire window through the designer?

Elazar Leibovich
  • 30,136
  • 27
  • 116
  • 161
  • If you just want to uset layout in Qt Designer, please ref to bellow tips: http://stackoverflow.com/questions/1508939/qt-layout-on-qmainwindow –  Jul 08 '13 at 05:23

5 Answers5

394

After creating your QVBoxLayout in Qt Designer, right-click on the background of your widget/dialog/window (not the QVBoxLayout, but the parent widget) and select Lay Out -> Lay Out in a Grid from the bottom of the context-menu. The QVBoxLayout should now stretch to fit the window and will resize automatically when the entire window is resized.

Greg S
  • 11,275
  • 2
  • 35
  • 48
  • Yeah I figured that out. But that's a bad default. What exactly happens when you drag the `QVBoxLayout` to the main window widget? – Elazar Leibovich Aug 16 '10 at 15:02
  • 6
    Thank you, I've been looking for a solution to this problem as well. – Simon H. Sep 17 '10 at 11:43
  • 38
    Actually, you do not need to create the Grid layout at all. Instead of creating a QVBoxLayout yourself, deselect all items and click on the background of the widget. Then select Lay Out -> Lay Out Vertically. – Michal Kottman Jan 12 '11 at 20:57
  • 3
    @Michal Kottman: That only works at design time. It will not change the layout when the user resizes the window at run time. – Jay Feb 04 '13 at 21:26
  • 5
    Both grid and vertical layout work for me at design and runtime. – AndiDog Feb 19 '13 at 08:15
  • 2
    I wish to add a little detail: if you wish to add a self-stretching layout to a **QTabWidget page**, you actually cannot right-click on the background of the page, as as you would select the QTabWidget and not the relevant tab. You have to right click on the tab label as shown in the designer window, and then select _Lay Out_ -> _Lay Out in a Grid_ – Emanuele Cipolla Jul 19 '14 at 09:06
  • 1
    I find it really weird that this worked. I already put my whole set of widgets inside a `QGridLayout` and I still had to put that in another grid to get the auto-resize feature to work... Looking at both grids, I just can't see any difference between either that would explain why my existing grid would not resize. (P.S. I am looking at the XML and just can't understand why things *fail* like that.) – Alexis Wilke Nov 02 '14 at 06:40
  • 1
    But why bothering to create another `QVBoxLayout` in a grid layout? This means the only child of the grid layout is the `QVBoxLayout` which will be considered as a redundance. So I am wondering if the best practice to create a layout for a widget is using the context-menu instead of drag a layout yourself? The only difference in the code level is the dragging way has one line extra to set `geometry` of the layout. So I don't see any necessary for the dragging way even to exist. – Joey.Z Dec 29 '14 at 07:21
  • Both does not work for me which is strange: I have tab widget and if I choose vertical layout then child shows in the default top left position of the parent widget, if I select grid layout - centered. However in next tab vertical layout works just fine. All of this is weird. Qt bug? – Aleksey Kontsevich Aug 05 '15 at 19:07
  • Thank you very much, I've been looking for this for some time. – kanha.vishva Apr 06 '20 at 16:53
17

According to the documentation, there needs to be a top level layout set.

A top level layout is necessary to ensure that your widgets will resize correctly when its window is resized. To check if you have set a top level layout, preview your widget and attempt to resize the window by dragging the size grip.

You can set one by clearing the selection and right clicking on the form itself and choosing one of the layouts available in the context menu.

Qt layouts

Mahmoud Hanafy
  • 7,162
  • 10
  • 42
  • 60
2

I've tried to find a "fit to screen" property but there is no such.

But setting widget's "maximumSize" to a "some big number" ( like 2000 x 2000 ) will automatically fit the widget to the parent widget space.

  • 6
    What happens with this technique on a MacBook Pro with Retina Display (2880x1800)? – ta.speot.is Nov 20 '12 at 04:57
  • 1
    Just set it to the maximum number it will accept (which Designer shows me is 16777215) and it should work on any monitor size. – andreb Aug 17 '13 at 21:17
1

Once you have add your layout with at least one widget in it, select your window and click the "Update" button of QtDesigner. The interface will be resized at the most optimized size and your layout will fit the whole window. Then when resizing the window, the layout will be resized in the same way.

Patrice Bernassola
  • 13,271
  • 3
  • 41
  • 54
  • I ment that when the user resizes the entire window, the vertical layout (and thus its content) will stretch to fit it. I don't care how it looks in the Qt Designer. – Elazar Leibovich Aug 16 '10 at 13:37
-7

Set the horizontalPolicy & VerticalPolicy for the controls/widgets to "Preferred".

Sunny
  • 1