问题是,当我调用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,但我仍然不能把它变成我自己的答案。我还看到了一些关于自定义内插器的东西,我需要做一个自定义内插器吗?如果需要,我该如何做。
发布于 2013-08-17 19:39:20
很酷的密码。它几乎工作,但动画并没有坚持通过enterEvent (虽然我不完全理解的力学)。如果你改变了
ani = QPropertyAnimation(self,"color")至
self.ani = QPropertyAnimation(self, "color")
# etc那就成功了。
发布于 2019-03-08 04:03:11
我很困惑,因为“动画”从来没有打印出来,但是ani.state()说动画正在运行。
在print的点,anmiation存在并正在运行。当Python从enterEvent返回时,ani就超出了范围。由于没有对对象的其他引用,Python垃圾收集对象,前提是不需要维护未引用的对象。由于对象被删除,动画永远不会执行。
这很有趣,我很想知道为什么动画必须是对象的属性。
接受的答案将ani更改为self.ani。此更改为作用域enterEvent以外的对象提供了参考。在经过更正的代码中,当enterEvent退出时,对象将维护对ani的引用,并且它不再是由于附加引用而回收的垃圾。当Qt返回到事件循环并成功执行动画时,它就存在了。
https://stackoverflow.com/questions/18291726
复制相似问题