1 回答
TA贡献1826条经验 获得超6个赞
属性(PyQt 中的pyqtProperty)仅在 QObjects 中有效,但在这种情况下QTreeWidgetItem不会从 QObject 继承,因此QPropertyAnimation也不起作用。因此,QPropertyAnimation您应该使用QVariantAnimation如下所示的,而不是使用:
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_())
添加回答
举报
