正如问题中所述,当我尝试使用 QGraphicsItemAnimation 的 setPosAt() 函数简单地对圆进行动画处理时,我在终端中收到此警告消息,并且我对此警告的起源感到非常困惑。我的代码:
def animate(self):
# moves item to location smoothly in one second
def animate_to(t,item,x,y):
# used to animate an item in specific ways
animation = QtGui.QGraphicsItemAnimation()
# create a timeline (1 sec here)
timeline = QtCore.QTimeLine(1000)
timeline.setFrameRange(0,100) # 100 steps
#item should at 'x,y' by time 't'
animation.setPosAt(t,QtCore.QPointF(x,y))
animation.setItem(item) # animate this item
animation.setTimeLine(timeline) # with this duration/steps
return animation
self.animations.append(animate_to(1,self.c1,150,150))
[ animation.timeLine().start() for animation in self.animations ]
self.animator.start(1000)
最让我困惑的是,当我注释掉上一节中的最后一行时,这个警告就消失了——根据我的理解,这与 QTimer 有关,而不是 QTimeLine 本身。作为引用,这是处理 QTimer 的唯一其他代码:
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
...
self.animator = QtCore.QTimer()
self.animator.timeout.connect(self.animate)
self.animate()
对此警告的起源或任何可能的修复有什么想法吗?
最佳答案
您的代码缩进有点偏离(顶行应该不缩进?),因此我将用文字总结您的代码,以确认我理解发生了什么。
您调用self.animate()
。此方法创建一个动画,并将其附加到列表 self.animations
中。您迭代此列表并启动动画。您启动一个计时器(超时 1 秒),该计时器调用 self.animate()
。
问题的出现是因为每次调用 self.animate()
时,self.animations
都会增加一个元素。因此,下次调用该方法时,旧的动画实例仍在列表中。您正在迭代整个列表以启动动画,因此您在动画上多次调用 animator.timeLine().start()
。
删除对计时器的调用可以防止 self.animation()
方法运行多次,因此当您注释掉该行时就不会遇到问题。
关于python - PySide 中出现 "QTimeLine already running"消息的原因是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22752822/