Python PyQT 5将小部件动态添加到扰流板

Python PyQT 5将小部件动态添加到扰流板,python,pyqt,pyqt5,Python,Pyqt,Pyqt5,我正在写一个小程序,理想情况下,我希望有一个固定大小的组,可以展开,其中我有更多的破坏者,代表我可以打开和关闭的项目,以便向我的系统添加一些实体 我一直在这里寻找类似的问题,并得出以下结论,以便半正常工作: 我添加了-按钮以从组中删除这些孩子,并添加了+按钮以将孩子添加到组中 只要我不删除或添加小部件,这似乎工作正常 我的代码如下所示: spoilers.py import sys from PyQt5.QtWidgets import * from PyQt5.QtWidgets impor

我正在写一个小程序,理想情况下,我希望有一个固定大小的组,可以展开,其中我有更多的破坏者,代表我可以打开和关闭的项目,以便向我的系统添加一些实体

我一直在这里寻找类似的问题,并得出以下结论,以便半正常工作:

我添加了-按钮以从组中删除这些孩子,并添加了+按钮以将孩子添加到组中

只要我不删除或添加小部件,这似乎工作正常

我的代码如下所示:

spoilers.py

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtGui import *
from PyQt5.QtCore import *


class Spoiler(QWidget):
    def __init__(self, parent=None, title='', animationDuration=300, addRemoveOption='None'):
        super(Spoiler, self).__init__(parent=parent)

        self.animationDuration = animationDuration
        self.toggleAnimation = QParallelAnimationGroup()
        self.contentArea = QScrollArea()
        self.headerLine = QFrame()
        self.toggleButton = QToolButton()
        self.mainLayout = QGridLayout()
        self.childWidgets = []

        self.toggleButton.setStyleSheet("QToolButton { border: none; }")
        self.toggleButton.setToolButtonStyle(Qt.ToolButtonTextBesideIcon)
        self.toggleButton.setArrowType(Qt.RightArrow)
        self.toggleButton.setText(str(title))
        self.toggleButton.setCheckable(True)
        self.toggleButton.setChecked(False)

        self.addRemoveOperation = addRemoveOption
        if addRemoveOption is not 'None':
            if addRemoveOption is 'Add':
                self.addRemoveButton = QPushButton('+')
            else:
                self.addRemoveButton = QPushButton('-')
            self.addRemoveButton.clicked.connect(self.onAddRemoveButton)


        self.contentArea.setStyleSheet("QScrollArea { background-color: white; border: none; }")
        self.contentArea.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed)
        # start out collapsed
        self.contentArea.setMaximumHeight(0)
        self.contentArea.setMinimumHeight(0)
        # let the entire widget grow and shrink with its content
        self.toggleAnimation.addAnimation(QPropertyAnimation(self, b"minimumHeight"))
        self.toggleAnimation.addAnimation(QPropertyAnimation(self, b"maximumHeight"))
        self.toggleAnimation.addAnimation(QPropertyAnimation(self.contentArea, b"maximumHeight"))
        # don't waste space
        self.mainLayout.setVerticalSpacing(0)
        self.mainLayout.setContentsMargins(0, 0, 0, 0)
        self.mainLayout.addWidget(self.toggleButton, 0, 0, 1, 1, Qt.AlignLeft)
        if addRemoveOption is not 'None':
            self.mainLayout.addWidget(self.addRemoveButton, 0, 2, 1, 1, Qt.AlignRight)
        self.mainLayout.addWidget(self.contentArea, 1, 0, 1, 3)
        self.setLayout(self.mainLayout)

        def start_animation(checked):
            arrow_type = Qt.DownArrow if checked else Qt.RightArrow
            direction = QAbstractAnimation.Forward if checked else QAbstractAnimation.Backward
            self.toggleButton.setArrowType(arrow_type)
            self.toggleAnimation.setDirection(direction)
            self.toggleAnimation.start()

        self.toggleButton.clicked.connect(start_animation)
        self.contentLayout = QVBoxLayout()
        self.setContentLayout(self.contentLayout)

    def setContentLayout(self, contentLayout):
        self.contentArea.destroy()
        self.contentArea.setLayout(contentLayout)
        collapsedHeight = self.sizeHint().height() - self.contentArea.maximumHeight()
        contentHeight = contentLayout.sizeHint().height()
        for i in range(self.toggleAnimation.animationCount()-1):
            spoilerAnimation = self.toggleAnimation.animationAt(i)
            spoilerAnimation.setDuration(self.animationDuration)
            spoilerAnimation.setStartValue(collapsedHeight)
            spoilerAnimation.setEndValue(collapsedHeight + contentHeight)
        contentAnimation = self.toggleAnimation.animationAt(self.toggleAnimation.animationCount() - 1)
        contentAnimation.setDuration(self.animationDuration)
        contentAnimation.setStartValue(0)
        contentAnimation.setEndValue(contentHeight)


    def addChild(self, child):
        self.childWidgets += [child]
        self.contentLayout.addWidget(child)
        self.setContentLayout(self.contentLayout)

    def removeChild(self, child):
        self.childWidgets.remove(child)
        #self.contentLayout.removeWidget(child)
        child.destroy()
        #self.setContentLayout(self.contentLayout)

    def onAddRemoveButton(self):
        self.addChild(LeafSpoiler(root=self))



class LeafSpoiler(Spoiler):
    def __init__(self, parent=None, root=None, title=''):
        if(root == None):
            addRemoveOption = 'None'
        else:
            addRemoveOption = 'Sub'
        super(LeafSpoiler, self).__init__(parent=parent, title=title, addRemoveOption=addRemoveOption)
        self.root = root

    def onAddRemoveButton(self):
        self.root.removeChild(self)
我不确定这为什么不起作用。我假设Spoiler.setContentLayout不应该被多次调用

如果有人能在这件事上帮助我,我会非常高兴的

您好,
我不太确定我是否正确理解了你的问题。我想你是说pyqt在试图移除扰流板时崩溃?至少这是我的机器上发生的事情

你的removeChild方法似乎是罪魁祸首。在不太了解崩溃来源的情况下,将其替换为调用以启用我的计算机上的子项删除:

class LeafSpoiler(Spoiler):
    # [...] same init as your's

    def onAddRemoveButton(self):
        self.deleteLater()
class LeafSpoiler(Spoiler):
    # [...] same init as your's

    def onAddRemoveButton(self):
        self.deleteLater()