我使用的是PySide2,下面的示例脚本生成警告:QAnimationGroup::animationAt: index is out of bounds
。
from PySide2 import QtCore
def add_animation(banner):
anim1 = QtCore.QPropertyAnimation(None, "geometry")
anim2 = QtCore.QPropertyAnimation(None, "geometry")
banner.addAnimation(anim1)
banner.addAnimation(anim2)
banner_animation = QtCore.QSequentialAnimationGroup(None)
add_animation(banner_animation)
# This is the line that generates the warning:
banner_animation.clear()
似乎如果我向组中添加多个动画并尝试清除它们,就会收到警告。添加单个动画不会导致警告。我已经用QT版本5.12、5.13和5.14进行了测试。
这是虫子还是我在这里做了什么奇怪的事?
啊,我想这可能是一个垃圾收集问题。anim1
和anim2
对象正在被收集,因为它们在方法退出时被清除,但Qt仍然保留对它们的引用。以下工作为例:
from PySide2 import QtCore
def add_animation(banner):
anim1 = QtCore.QPropertyAnimation(None, "geometry")
anim2 = QtCore.QPropertyAnimation(None, "geometry")
banner.addAnimation(anim1)
banner.addAnimation(anim2)
return anim1, anim2
banner_animation = QtCore.QSequentialAnimationGroup(None)
anim1, anim2 = add_animation(banner_animation)
banner_animation.clear()