PySide QPropertyImation未启动

2024-09-29 21:35:42 发布

您现在位置:Python中文网/ 问答频道 /正文

问题是当我调用QPropertyAnimation.start()时,什么都没有发生。在

颜色是我制作动画的属性,button是类。在

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,但我还是不能把它变成自己的答案。我还看到了一些关于自定义插值器的东西,我需要做一个自定义插值器吗,如果是的话,我该怎么做呢。在


Tags: textselfeventinitvaluedefbutton动画
2条回答

很酷的代码。它几乎可以工作,但动画不会持续超过事件(虽然我不完全理解机制)如果你改变

ani = QPropertyAnimation(self,"color")

^{pr2}$

那它就行了。在

I'm confused because "animating" never prints out, but ani.state() says the animation is running.

print的点上,存在并正在运行。当Python 从enterEvent返回,ani超出范围。因为没有别的了 对对象的引用,Python垃圾收集对象时假定 不需要维护未被引用的对象。因为目标是 删除后,动画永远不会执行。在

Thats interesting, I'd love to know why the animation has to be a property of the object.

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

相关问题 更多 >

    热门问题