如何让Qt Widget随着窗口大小而增长?
我想要一个小的 QFormLayout
,它会增长以填充其父窗口小部件。
我使用 Qt Designer 中的 QWidget
模板创建了一个新的 .ui 文件。我在该“窗口”内放置了一个QFormLayout
,然后在该QFormLayout
内放置了一些控件。
这一切都工作得相当好,但 QFormLayout 始终保持我在 Qt Designer 中设置的大小。我希望 QFormLayout 填充其父窗口小部件并随之增长/收缩。
我怎样才能做到这一点?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在Designer 中,激活centralWidget 并指定布局,例如水平或垂直布局。
然后你的 QFormLayout 将自动调整大小。
始终确保所有小部件都有布局!否则,自动调整大小将破坏该小部件!
参见
In Designer, activate the centralWidget and assign a layout, e.g. horizontal or vertical layout.
Then your QFormLayout will automatically resize.
Always make sure, that all widgets have a layout! Otherwise, automatic resizing will break with that widget!
See also
我发现除非我在中央小部件下方添加了至少一个子级,否则无法为中央小部件分配布局。然后我可以突出显示带有红色“禁用”标记的小图标,然后单击顶部设计器工具栏中的布局。
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.
接受的答案(其图像)是错误的,至少现在在 QT5 中是这样。相反,您应该为根对象/小部件分配一个布局(指向上述图像,它应该是
MainWindow
而不是centralWidget
)。另请注意,您必须在其下至少创建一个QObject
才能使其正常工作。执行此操作,您的用户界面将响应窗口大小调整。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 ofcentralWidget
). Also note that you must have at least oneQObject
created beneath it for this to work. Do this and your ui will become responsive to window resizing.您需要通过按与布局相关的工具栏按钮之一,将顶级 QWidget 对象的默认布局类型从中断布局类型更改为其他布局类型(垂直布局、水平布局、网格布局、表单布局)。
例如:
像这样:
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:
To something like this: