如何在 Qt 中使用间隔符 [英] How to use spacers in Qt

查看:106
本文介绍了如何在 Qt 中使用间隔符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 QGroupBox 分配给它 QVBoxLayout.

I have a QGroupBox with assigned to it QVBoxLayout.

groupBox = QtGui.QGroupBox()
layout = QtGui.QVBoxLayout()
groupBox.setLayout(layout )

然后有一个 QLabel 分配给布局.

Then there is a single QLabel assigned to the layout.

label = QtGui.QLabel()   
layout.addWidget(label)

问题是QLabel定位在GroupBox的中间.虽然我希望它与 GroupBox 的顶部边缘对齐.我可能需要添加某种间隔.因此,间隔器会动态调整自身大小以填充 GroupBox 的空白区域,向上推动 QLabel.

The problem is that QLabel is positioned at the middle of the GroupBox. While I want it to be aligned with the top edge of the GroupBox. I probably need to add some sort of spacer. So the spacer would dynamically resize itself to fill the GroupBox's empty area pushing QLabel upward.

应该使用什么小部件和什么尺寸策略作为间隔?

What widget and what size policy should be used as a spacer?

推荐答案

label = QtGui.QLabel()   
layout.addWidget(label)
verticalSpacer = QtGui.QSpacerItem(20, 40, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding)
layout.addItem(verticalSpacer)

这应该有效

这篇关于如何在 Qt 中使用间隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆