PySide QPropertyAnimation无法启动

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

问题是,当我打电话给QPropertyAnimation.start()时,没有任何反应。

颜色是我动画的属性,按钮是类。

class Button(QPushButton):
    def __init__(self,text="",parent=None):
        super(Button,self).__init__(text,parent)
        # ...
        self.innercolor = QColor(200,0,20)

    def setcolor(self,value): self.innercolor = value
    def getcolor(self): return self.innercolor
    color = Property(QColor,getcolor,setcolor)

    def paintEvent(self, event):
        p = QPainter(self)
        p.fillRect(self.rect(),self.color)
        # ...
        p.end()

    def animated(self,value): print "animating"; self.update()

    def enterEvent(self, event):
        ani = QPropertyAnimation(self,"color")
        ani.setStartValue(self.color)
        ani.setEndValue(QColor(0,0,10))
        ani.setDuration(2000)
        ani.valueChanged.connect(self.animated)
        ani.start()
        print ani.state()
        return QPushButton.enterEvent(self, event)

我很困惑,因为"animating"从未打印出来,但ani.state()说动画正在运行。

我不是要求调试我的代码或任何东西,但我认为必须有一些我缺少的东西,无论是在我的代码中还是在我对QPropertyAnimation的使用的理解中。

我搜索谷歌的答案,但没有出现,反正我没有任何关系。我发现的最接近的是another SO question,但我仍然无法将其转化为自己的答案。我还看到了关于自定义插补器的一些内容,我是否需要制作自定义插补器,如果是这样,我该怎么做。

python qt animation colors pyside
2个回答
1
投票

很酷的代码。它几乎可以工作,但动画并没有持续通过enterEvent(虽然我并不完全理解它的机制。)如果你改变了

ani = QPropertyAnimation(self,"color")

self.ani = QPropertyAnimation(self, "color")
# etc

然后它会工作。


0
投票

我很困惑,因为“动画”永远不会打印出来,但ani.state()表示动画正在运行。

print,点存在并且正在运行。当Python从enterEvent返回时,ani超出了范围。由于没有对该对象的其他引用,Python垃圾收集该对象,假设不需要维护未引用的对象。由于删除了对象,因此动画永远不会执行。

多数民众赞成有趣,我很想知道为什么动画必须是对象的属性。

接受的答案将ani改为self.ani。此更改为范围enterEvent之外的对象提供了参考。在更正后的代码中,当enterEvent退出时,该对象保持对ani的引用,并且由于附加引用而不再对其进行垃圾回收。当Qt返回事件循环并且动画成功执行时,它就存在。

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