如何使一个 Qt 小部件随着窗口大小增长?

我想有一个小的 QFormLayout,增长,以填补其父窗口小部件。

我使用 Qt Designer 中的 QWidget模板创建了一个新的 。 ui文件。我在那个“窗口”里面放了一个 QFormLayout,然后在那个 QFormLayout里面放了一些控件。

这一切都工作得相当不错,但是 QFormLayout始终保持在我在 Qt Designer 中设置的大小。我希望 QFormLayout能够填充它的父部件,并用它来增长/收缩。

我怎么才能做到呢?

162595 次浏览

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

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.

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.

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