如何在 PyQt 中创建可折叠框 [英] How to create collapsible box in PyQt

查看:221
本文介绍了如何在 PyQt 中创建可折叠框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 PyQt4,我正在尝试创建一个可折叠的框,其中将包含几个子小部件,其中已经创建了子小部件并使用 QVboxLayout

我该如何创建它?目前我无法找到任何命令,例如.QCollapseBox

如果没有展开:

+ 可折叠框标题

如果展开:

- 可折叠框标题|- Widget01|- Widget02

如果有 + 或 - 符号,或者可以帮助确定它是否已展开的箭头符号

解决方案

以@的

PyQt5 版本

from PyQt5 import QtCore, QtGui, QtWidgets类 CollapsibleBox(QtWidgets.QWidget):def __init__(self, title="", parent=None):super(CollapsibleBox, self).__init__(parent)self.toggle_button = QtWidgets.QToolButton(文本=标题,可检查=真,检查=假)self.toggle_button.setStyleSheet("QToolButton { border: none; }")self.toggle_button.setToolButtonStyle(QtCore.Qt.ToolButtonTextBesideIcon)self.toggle_button.setArrowType(QtCore.Qt.RightArrow)self.toggle_button.pressed.connect(self.on_pressed)self.toggle_animation = QtCore.QParallelAnimationGroup(self)self.content_area = QtWidgets.QScrollArea(最大高度=0,最小高度=0)self.content_area.setSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Fixed)self.content_area.setFrameShape(QtWidgets.QFrame.NoFrame)躺 = QtWidgets.QVBoxLayout(self)设置间距(0)lay.setContentsMargins(0, 0, 0, 0)Lay.addWidget(self.toggle_button)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()def on_pressed(self):选中 = self.toggle_button.isChecked()self.toggle_button.setArrowType(QtCore.Qt.DownArrow 如果没有选中否则 QtCore.Qt.RightArrow)self.toggle_animation.setDirection(QtCore.QAbstractAnimation.Forward如果没有检查否则 QtCore.QAbstractAnimation.Backward)self.toggle_animation.start()def setContentLayout(self, layout):躺 = self.content_area.layout()延迟self.content_area.setLayout(布局)折叠高度 = (self.sizeHint().height() - self.content_area.maximumHeight())content_height = layout.sizeHint().height()对于我在范围内(self.toggle_animation.animationCount()):动画 = self.toggle_animation.animationAt(i)动画.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)如果 __name__ == "__main__":导入系统随机导入app = QtWidgets.QApplication(sys.argv)w = QtWidgets.QMainWindow()w.setCentralWidget(QtWidgets.QWidget())Dock = QtWidgets.QDockWidget("可折叠演示")w.addDockWidget(QtCore.Qt.LeftDockWidgetArea,dock)滚动 = QtWidgets.QScrollArea()码头.setWidget(滚动)内容 = QtWidgets.QWidget()scroll.setWidget(内容)scroll.setWidgetResizable(True)vlay = QtWidgets.QVBoxLayout(content)对于范围内的我(10):box = CollapsibleBox("Collapsible Box Header-{}".format(i))vlay.addWidget(box)躺 = QtWidgets.QVBoxLayout()对于范围内的 j(8):label = QtWidgets.QLabel("{}".format(j))color = QtGui.QColor(*[random.randint(0, 255) for _ in range(3)])label.setStyleSheet(背景颜色:{};颜色:白色;".format(color.name()))label.setAlignment(QtCore.Qt.AlignCenter)Lay.addWidget(标签)box.setContentLayout(lay)vlay.addStretch()w.resize(640, 480)w.show()sys.exit(app.exec_())

I am using PyQt4, and I am trying to create a collapsible box where it will contains a couple of child widgets where the child widgets are already created and layout using a QVboxLayout

How do I go about creating it? Currently I am unable to find any commands eg. QCollapseBox etc.

If it is not expanded:

+ Collapsible Box Header

If expanded:

- Collapsible Box Header
|- Widget01
|- Widget02

Where there is a + or - sign, or an arrow sign that can helps determine if it has been expanded or not

解决方案

Using as base the logic that is implemented in the solution of @xsquared modifying certain parts we obtain the following:

PyQt4 version

from PyQt4 import QtCore, QtGui


class CollapsibleBox(QtGui.QWidget):
    def __init__(self, title="", parent=None):
        super(CollapsibleBox, self).__init__(parent)

        self.toggle_button = QtGui.QToolButton(
            text=title, checkable=True, checked=False
        )
        self.toggle_button.setStyleSheet("QToolButton { border: none; }")
        self.toggle_button.setToolButtonStyle(
            QtCore.Qt.ToolButtonTextBesideIcon
        )
        self.toggle_button.setArrowType(QtCore.Qt.RightArrow)
        self.toggle_button.pressed.connect(self.on_pressed)

        self.toggle_animation = QtCore.QParallelAnimationGroup(self)

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

        lay = QtGui.QVBoxLayout(self)
        lay.setSpacing(0)
        lay.setContentsMargins(0, 0, 0, 0)
        lay.addWidget(self.toggle_button)
        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()
    def on_pressed(self):
        checked = self.toggle_button.isChecked()
        self.toggle_button.setArrowType(
            QtCore.Qt.DownArrow if not checked else QtCore.Qt.RightArrow
        )
        self.toggle_animation.setDirection(
            QtCore.QAbstractAnimation.Forward
            if not 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 = QtGui.QApplication(sys.argv)

    w = QtGui.QMainWindow()
    w.setCentralWidget(QtGui.QWidget())
    dock = QtGui.QDockWidget("Collapsible Demo")
    w.addDockWidget(QtCore.Qt.LeftDockWidgetArea, dock)
    scroll = QtGui.QScrollArea()
    dock.setWidget(scroll)
    content = QtGui.QWidget()
    scroll.setWidget(content)
    scroll.setWidgetResizable(True)

    vlay = QtGui.QVBoxLayout(content)

    for i in range(10):
        box = CollapsibleBox("Collapsible Box Header-{}".format(i))
        vlay.addWidget(box)
        lay = QtGui.QVBoxLayout()
        for j in range(8):
            label = QtGui.QLabel("{}".format(j))
            color = QtGui.QColor(*[random.randint(0, 255) for _ in range(3)])
            label.setStyleSheet(
                "background-color: {}; color : white;".format(color.name())
            )
            label.setAlignment(QtCore.Qt.AlignCenter)
            lay.addWidget(label)

        box.setContentLayout(lay)
    vlay.addStretch()
    w.resize(640, 480)
    w.show()

    sys.exit(app.exec_())

PyQt5 version

from PyQt5 import QtCore, QtGui, QtWidgets


class CollapsibleBox(QtWidgets.QWidget):
    def __init__(self, title="", parent=None):
        super(CollapsibleBox, self).__init__(parent)

        self.toggle_button = QtWidgets.QToolButton(
            text=title, checkable=True, checked=False
        )
        self.toggle_button.setStyleSheet("QToolButton { border: none; }")
        self.toggle_button.setToolButtonStyle(
            QtCore.Qt.ToolButtonTextBesideIcon
        )
        self.toggle_button.setArrowType(QtCore.Qt.RightArrow)
        self.toggle_button.pressed.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.toggle_button)
        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()
    def on_pressed(self):
        checked = self.toggle_button.isChecked()
        self.toggle_button.setArrowType(
            QtCore.Qt.DownArrow if not checked else QtCore.Qt.RightArrow
        )
        self.toggle_animation.setDirection(
            QtCore.QAbstractAnimation.Forward
            if not 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()
    w.setCentralWidget(QtWidgets.QWidget())
    dock = QtWidgets.QDockWidget("Collapsible Demo")
    w.addDockWidget(QtCore.Qt.LeftDockWidgetArea, dock)
    scroll = QtWidgets.QScrollArea()
    dock.setWidget(scroll)
    content = QtWidgets.QWidget()
    scroll.setWidget(content)
    scroll.setWidgetResizable(True)
    vlay = QtWidgets.QVBoxLayout(content)
    for i in range(10):
        box = CollapsibleBox("Collapsible Box Header-{}".format(i))
        vlay.addWidget(box)
        lay = QtWidgets.QVBoxLayout()
        for j in range(8):
            label = QtWidgets.QLabel("{}".format(j))
            color = QtGui.QColor(*[random.randint(0, 255) for _ in range(3)])
            label.setStyleSheet(
                "background-color: {}; color : white;".format(color.name())
            )
            label.setAlignment(QtCore.Qt.AlignCenter)
            lay.addWidget(label)

        box.setContentLayout(lay)
    vlay.addStretch()
    w.resize(640, 480)
    w.show()
    sys.exit(app.exec_())

这篇关于如何在 PyQt 中创建可折叠框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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