【问题标题】:PySide QPropertyAnimation Not StartingPySide QPropertyAnimation 未启动
【发布时间】:2013-08-17 17:53:54
【问题描述】:

问题是当我打电话给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


【解决方案1】:

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

ani = QPropertyAnimation(self,"color")

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

然后它会工作。

【讨论】:

  • 这很有趣,我很想知道为什么动画必须是对象的属性。是否与anienterEvent 范围之外不可用有关?
【解决方案2】:

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

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

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

接受的答案将ani 更改为self.ani。这种变化提供了一个 对enterEvent 范围之外的对象的引用。在更正后的代码中, 当enterEvent 退出时,对象保持对ani 的引用,它是 由于额外的参考,不再收集垃圾。它存在于 Qt 返回事件循环,动画成功执行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多