【问题标题】:How to dynamically change the dimensions of a panel in PySide QT?如何在 PySide QT 中动态更改面板的尺寸?
【发布时间】:2014-06-17 08:34:42
【问题描述】:

我的应用程序窗口有两个部分。顶部由 QVerticalLayout 中包含的几个小部件组成,而 QVerticalLayout 又包含在 QVerticalSpacer 中。这部分始终可见。

底部窗格包含在 GroupBox 中,当用户询问时可能会变得不可见。在屏幕底部添加了一个复选框,吹组框。单击它通过调用它的 hide() 和 show() 方法来切换组框的可见性。

现在,当底部窗格被隐藏时,我该怎么做才能缩小它的高度,尽可能接近零?或者包含这两个部分的选项卡的尺寸?还是整个 QMainWindow?

试图玩弄我的 groupbox 的 SizePolicy。 VerticalPolicy 是这里有趣的属性。尝试将其设置为 Fixed,然后在调用 groupbox.hide() 后调用 groupbox.setGeometry()。似乎以这种方式我无法将组框缩小到 10 像素左右。不能使它小于其内容的高度。

我是否应该在隐藏组框时从其内容中删除它并重新填充它以响应显示?希望有更好的办法。

【问题讨论】:

标签: qt python-2.7 pyside


【解决方案1】:

在布局上使用非默认大小约束:

layout.setSizeConstraint(QtGui.QLayout.SetFixedSize)

或者隐藏后调用adjustSize:

widget.adjustSize()

一个 PySide 的小例子:

from PySide import QtGui

app = QtGui.QApplication([])
window = QtGui.QWidget()
window_layout = QtGui.QVBoxLayout(window)
window_layout.setSizeConstraint(QtGui.QLayout.SetFixedSize) # either this line
button = QtGui.QPushButton('Hide the box', window)
window_layout.addWidget(button)
box = QtGui.QGroupBox('A group')
box_layout = QtGui.QVBoxLayout(box)
box_layout.addWidget(QtGui.QLabel('Some text', box))
window_layout.addWidget(box)

def adjust():
    box.hide()
    window.adjustSize() # or that line do the trick

button.clicked.connect(adjust)

window.show()
app.exec_()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-30
    • 1970-01-01
    • 2014-01-11
    • 2021-11-06
    • 1970-01-01
    • 2018-09-11
    • 2015-02-16
    • 1970-01-01
    相关资源
    最近更新 更多