1

I need to add color animation to QTreeWidgetItem, but in my code, it raise some error, can anyone help me?

The code sample is here:

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)

and call method like this:

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()

erros here:

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'  
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
jett chen
  • 1,067
  • 16
  • 33

1 Answers1

1

The Property(pyqtProperty in PyQt) are only valid in the QObjects, but in this case QTreeWidgetItem does not inherit from QObject so the QPropertyAnimation will not work either. So instead of using QPropertyAnimation you should use QVariantAnimation as I show below:

import os
from PyQt5 import QtCore, QtGui, QtWidgets


class TreeWidgetItem(QtWidgets.QTreeWidgetItem):
    @property
    def animation(self):
        if not hasattr(self, "_animation"):
            self._animation = QtCore.QVariantAnimation()
            self._animation.valueChanged.connect(self._on_value_changed)
        return self._animation

    def _on_value_changed(self, color):
        for i in range(self.columnCount()):
            self.setBackground(i, color)


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)

    w = QtWidgets.QTreeWidget(columnCount=2)

    it = TreeWidgetItem(["Foo", "Bar"])

    # setup animation
    it.animation.setStartValue(QtGui.QColor("white"))
    it.animation.setEndValue(QtGui.QColor("red"))
    it.animation.setDuration(5 * 1000)
    it.animation.start()

    # add item to QTreeWidget
    w.addTopLevelItem(it)

    w.show()
    sys.exit(app.exec_())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • your suggest work fine, buf if i change my origin code as `class TreeWidgetItem(QTreeWidgetItem, QObject)` to inherit QObject, i think it will also work, but i run my code in cmd line, the app gone away without any error? – jett chen Oct 18 '19 at 07:09
  • 1
    @jie Multiple inheritance in PyQt is limited for some cases and QTreeWidgetItem is not within them: https://www.riverbankcomputing.com/static/Docs/PyQt5/gotchas.html#multiple-inheritance – eyllanesc Oct 18 '19 at 07:17
  • i had click up arrow before , for a long time i think it is to mark good answer, today i had see a check label below the down-arrow. – jett chen Nov 01 '19 at 07:21