问题是当我调用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,但我还是不能把它变成自己的答案。我还看到了一些关于自定义插值器的东西,我需要做一个自定义插值器吗,如果是的话,我该怎么做呢。在
很酷的代码。它几乎可以工作,但动画不会持续超过事件(虽然我不完全理解机制)如果你改变
到
^{pr2}$那它就行了。在
在
print
的点上,存在并正在运行。当Python 从enterEvent
返回,ani
超出范围。因为没有别的了 对对象的引用,Python垃圾收集对象时假定 不需要维护未被引用的对象。因为目标是 删除后,动画永远不会执行。在接受的答案将
ani
更改为self.ani
。此更改提供了 引用范围enterEvent
之外的对象。在更正后的代码中, 当enterEvent
退出时,对象保持对ani
的引用,并且 由于附加引用,不再进行垃圾回收。它存在于Qt 返回到事件循环,动画成功执行。在相关问题 更多 >
编程相关推荐