首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >QPropertyAnimation on QGraphicsPathItem

QPropertyAnimation on QGraphicsPathItem
EN

Stack Overflow用户
提问于 2021-04-06 18:30:13
回答 1查看 180关注 0票数 0

我正在尝试动画的颜色,一个QGraphicsPathItem。在Qt文档中,他们说,如果您要对要动画的项进行子类,则可以对QGraphicsItem进行动画化。这是Qt文档中(来自动画与图形视图框架)的内容:

当您想要动画QGraphicsItems时,您也使用QPropertyAnimation。但是,QGraphicsItem不继承QObject。一个好的解决方案是对你想要动画的图形项进行子类化。然后,该类还将继承QObject。这样,QPropertyAnimation就可以用于QGraphicsItems。下面的示例显示了如何做到这一点。另一种可能是继承QGraphicsWidget,它已经是一个QObject。 注意,QObject必须是继承的第一个类,因为元对象系统需要这样做。

我试图这样做,但由于这个原因,我的程序在创建一个新的“Edge”类时会崩溃。

使用QObject的My类:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2021-04-06 18:46:26

QObject继承的概念不适用于python,因为多重继承仅在某些情况下可用,而QGraphicsItem则不适用。一种可能的解决方案是使用QVariantAnimation组合来实现动画。

代码语言:javascript
复制
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)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66974467

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档