如何向QTreeWidgetItem添加彩色动画?

问题描述 投票:0回答:1

我需要向QTreeWidgetItem添加彩色动画,但是在我的代码中,它会引发一些错误,有人可以帮助我吗?

代码示例在这里:

class TreeWigetItem(QTreeWidgetItem):
    def __init__(self, parent=None):
        super().__init__(parent)

    @pyqtProperty(QBrush)
    def bcolor(self):
        return self.background(0)

    @bcolor.setter
    def bcolor(self, color):
        self.setBackground(0, color)
        self.setBackground(1, color)

和这样的调用方法:

child_item = TreeWigetItem()
self.child_item_ani = QPropertyAnimation(child_item, b'bcolor')
self.child_item_ani.setDuration(1000)
self.child_item_ani.setEndValue(QBrush(Qt.red))
self.child_item_ani.start()

这里的错误:

self.child_item_ani = QPropertyAnimation(child_item, b'bcolor')  
TypeError: arguments did not match any overloaded call:  
  QPropertyAnimation(parent: QObject = None): argument 1 has unexpected type 'TreeWigetItem'  
  QPropertyAnimation(QObject, Union[QByteArray, bytes, bytearray], parent: QObject = None): argument 1 has unexpected type 'TreeWigetItem'  
python pyqt pyqt5
1个回答
0
投票

Property(PyQt中的pyqtProperty)仅在QObjects中有效,但是在这种情况下,QTreeWidgetItem不会从QObject继承,因此QTreeWidgetItem也将不起作用。因此,代替使用QPropertyAnimation,您应该使用QPropertyAnimation,如下所示:

QPropertyAnimation
© www.soinside.com 2019 - 2024. All rights reserved.