ChatGPT解决这个技术问题 Extra ChatGPT

Auto-expanding layout with Qt-Designer

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?

If you just want to uset layout in Qt Designer, please ref to bellow tips: stackoverflow.com/questions/1508939/qt-layout-on-qmainwindow

G
Greg S

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.


Thank you, I've been looking for a solution to this problem as well.
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: That only works at design time. It will not change the layout when the user resizes the window at run time.
Both grid and vertical layout work for me at design and runtime.
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
M
Mahmoud Hanafy

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.

https://i.stack.imgur.com/6VO9C.png


T
Tero Nivala

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.


What happens with this technique on a MacBook Pro with Retina Display (2880x1800)?
Just set it to the maximum number it will accept (which Designer shows me is 16777215) and it should work on any monitor size.
P
Patrice Bernassola

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.


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.
S
Sunny

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