正如问题中所述,当我尝试使用QGraphicsSiteManimation的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的唯一其他代码:
^{pr2}$对这一警告的起源有什么想法或任何可能的解决办法?在
您的代码缩进有点偏离(顶行应该取消缩进吗?),所以我将用文字总结您的代码,以确认我理解发生了什么。在
您调用
self.animate()
。此方法创建一个动画,并将其附加到列表self.animations
。迭代这个列表并开始动画。启动一个计时器(超时1秒),它调用self.animate()
。在问题的出现是因为每次调用
self.animations
时,self.animations
会增长一个元素。因此,下次调用该方法时,旧动画实例仍在列表中。您正在遍历整个列表以启动动画,因此在一个动画上多次调用animator.timeLine().start()
。在删除对计时器的调用可防止
self.animation()
方法多次运行,因此在注释掉该行时不会遇到问题。在相关问题 更多 >
编程相关推荐