我正在尝试动画的颜色,一个QGraphicsPathItem。在Qt文档中,他们说,如果您要对要动画的项进行子类,则可以对QGraphicsItem进行动画化。这是Qt文档中(来自动画与图形视图框架)的内容:
当您想要动画QGraphicsItems时,您也使用QPropertyAnimation。但是,QGraphicsItem不继承QObject。一个好的解决方案是对你想要动画的图形项进行子类化。然后,该类还将继承QObject。这样,QPropertyAnimation就可以用于QGraphicsItems。下面的示例显示了如何做到这一点。另一种可能是继承QGraphicsWidget,它已经是一个QObject。 注意,QObject必须是继承的第一个类,因为元对象系统需要这样做。
我试图这样做,但由于这个原因,我的程序在创建一个新的“Edge”类时会崩溃。
使用QObject的My类:
class Edge(QObject, QGraphicsPathItem):
def __init__(self, point1, point2):
super().__init__()
self.point1 = point1
self.point2 = point2
self.setPen(QPen(Qt.white, 2, Qt.SolidLine))
def create_path(self, radius):
path = QPainterPath()
path.moveTo(self.point1.x() + radius, self.point1.y() + radius)
path.lineTo(self.point2.x() + radius, self.point2.y() + radius)
return path发布于 2021-04-06 18:46:26
QObject继承的概念不适用于python,因为多重继承仅在某些情况下可用,而QGraphicsItem则不适用。一种可能的解决方案是使用QVariantAnimation组合来实现动画。
class Edge(QGraphicsPathItem):
def __init__(self, point1, point2):
super().__init__()
self.point1 = point1
self.point2 = point2
self.setPen(QPen(Qt.white, 2, Qt.SolidLine))
self.animation = QVariantAnimation()
self.animation.valueChanged.connect(self.handle_valueChanged)
self.animation.setStartValue(QColor("blue"))
self.animation.setEndValue(QColor("red"))
self.animation.setDuration(1000)
def create_path(self, radius):
path = QPainterPath()
path.moveTo(self.point1.x() + radius, self.point1.y() + radius)
path.lineTo(self.point2.x() + radius, self.point2.y() + radius)
return path
def start_animation(self):
self.animation.start()
def handle_valueChanged(self, value):
self.setPen(QPen(value), 2, Qt.SolidLine)https://stackoverflow.com/questions/66974467
复制相似问题