1

The problem is that when I call QPropertyAnimation.start(), nothing happens.

Color is the property I'm animating and button is the class.

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)

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

I'm not asking to debug my code or anything, but I think there must be something I'm missing, either in my code or in my understanding of the use of QPropertyAnimation.

I've searched google for an answer, but nothing came up, nothing relevant to me anyway. The closest I found was another SO question, but I still couldn't turn that into an answer for myself. I also saw something about a custom interpolator, do I need to make a custom interpolator, if so, how do I do that.

4

2 回答 2

2

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

ani = QPropertyAnimation(self,"color")

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

那么它将起作用。

于 2013-08-17T19:39:20.107 回答
0

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

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

这很有趣,我很想知道为什么动画必须是对象的属性。

接受的答案ani变为self.ani。此更改提供了对范围外对象的引用enterEvent。在更正后的代码中,当enterEvent对象退出时,它会维护一个对的引用,ani并且由于额外的引用,它不再被垃圾收集。当 Qt 返回事件循环并且动画成功执行时,它就存在。

于 2019-03-08T04:03:11.117 回答