【问题标题】:Qt layout does not reflect groupbox size changeQt 布局不反映组框大小变化
【发布时间】:2019-02-19 07:38:31
【问题描述】:

我正在尝试使用 PyQt5 和 QT Creator 制作带有动画可折叠 groupBox 的 UI。

如果 groupBox 未选中,它的高度会缩小到某个较小的值, 如果选中groupBox,则其高度将扩展为sizeHint().height()

问题是当另一个groupBox 出现在布局中时。 anothergroupBox 位置并未反映折叠的groupBox 大小已更改。

有没有办法强制底部groupBox随着折叠groupBox移动?

如下图:


附加信息

界面布局:

groupBox 调整大小实现:

my_ui._ui.groupBox.toggled.connect(my_ui.group_box_size_change)

def group_box_size_change(self):
    duration = 1000
    self.animaiton_gb = QtCore.QPropertyAnimation(self._ui.groupBox, b"size")
    self.animaiton_gb.setDuration(duration)

    self.animaiton_gb.setStartValue(QtCore.QSize(self._ui.groupBox.width(),  self._ui.groupBox.height()))

    if self._ui.groupBox.isChecked():
        self.animaiton_gb.setEndValue(QtCore.QSize(self._ui.groupBox.width(), self._ui.groupBox.sizeHint().height()))
    else:
        self.animaiton_gb.setEndValue(QtCore.QSize(self._ui.groupBox.width(), 49))

    self.animaiton_gb.start()

【问题讨论】:

  • 请发布一个最小示例来演示问题。
  • 我用 gif 替换了最后一张图片,以表明当上层 groupBox 折叠时,下层 groupBox 保持在同一位置。我希望下部 groupBox 向上移动以折叠上部 groupBox,以便它们之间的空间最小。
  • 请发布一个最小示例来演示问题minimal reproducible example
  • 我重新排列了文本,所以问题从动机开始,然后是当前实施、问题陈述和问题本身。下面是问题的直观表示,以便于理解。新创建的“附加信息”部分应该为那些愿意回答我的问题并需要更多信息的人服务。我希望这种格式更具可读性。
  • 尝试将 GroupBox 对象放在 QVBoxLayout 中。那应该自动处理它们的间距和定位。您可能需要选择调整大小事件或类似事件,但我认为它应该可以工作。 layout management 上的文档很不错。

标签: python pyqt pyqt5 qvboxlayout


【解决方案1】:

考虑到my old answer 需要类似小部件的问题,以下是解决方案。在这种情况下,策略是使用 QScrollArea 作为容器并使用 minimumHeight 和 maximumHeight 属性。

from PyQt5 import QtCore, QtGui, QtWidgets


class CollapsibleBox(QtWidgets.QGroupBox):
    def __init__(self, title="", parent=None):
        super(CollapsibleBox, self).__init__(title, parent)
        self.setCheckable(True)
        self.setChecked(False)
        self.toggled.connect(self.on_pressed)
        self.toggle_animation = QtCore.QParallelAnimationGroup(self)

        self.content_area = QtWidgets.QScrollArea(maximumHeight=0, minimumHeight=0)
        self.content_area.setSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Fixed)
        self.content_area.setFrameShape(QtWidgets.QFrame.NoFrame)

        lay = QtWidgets.QVBoxLayout(self)
        lay.setSpacing(0)
        lay.setContentsMargins(0, 0, 0, 0)
        lay.addWidget(self.content_area)

        self.toggle_animation.addAnimation(QtCore.QPropertyAnimation(self, b"minimumHeight"))
        self.toggle_animation.addAnimation(QtCore.QPropertyAnimation(self, b"maximumHeight"))
        self.toggle_animation.addAnimation(QtCore.QPropertyAnimation(self.content_area, b"maximumHeight"))

    @QtCore.pyqtSlot(bool)
    def on_pressed(self, checked):
        self.toggle_animation.setDirection(QtCore.QAbstractAnimation.Forward if checked else QtCore.QAbstractAnimation.Backward)
        self.toggle_animation.start()

    def setContentLayout(self, layout):
        lay = self.content_area.layout()
        del lay
        self.content_area.setLayout(layout)
        collapsed_height = self.sizeHint().height() - self.content_area.maximumHeight()
        content_height = layout.sizeHint().height()
        for i in range(self.toggle_animation.animationCount()):
            animation = self.toggle_animation.animationAt(i)
            animation.setDuration(500)
            animation.setStartValue(collapsed_height)
            animation.setEndValue(collapsed_height + content_height)

        content_animation = self.toggle_animation.animationAt(self.toggle_animation.animationCount() - 1)
        content_animation.setDuration(500)
        content_animation.setStartValue(0)
        content_animation.setEndValue(content_height)

if __name__ == '__main__':
    import sys
    import random

    app = QtWidgets.QApplication(sys.argv)
    w = QtWidgets.QMainWindow()
    scroll = QtWidgets.QScrollArea()
    content = QtWidgets.QWidget()
    scroll.setWidget(content)
    scroll.setWidgetResizable(True)
    vlay = QtWidgets.QVBoxLayout(content)
    counter = 0
    for i in range(10):
        box = CollapsibleBox("Collapsible Box Header-{}".format(i))
        vlay.addWidget(box)
        lay = QtWidgets.QVBoxLayout()
        for j in range(8):
            btn = QtWidgets.QPushButton("PushButton-{}".format(counter))       
            lay.addWidget(btn)
            counter += 1
        box.setContentLayout(lay)
    vlay.addStretch()
    w.setCentralWidget(scroll)
    w.resize(240, 480)
    w.show()

    sys.exit(app.exec_())

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多