Domanda

As stated in the question, I'm getting this warning message in my terminal when I try to simply animate a circle using QGraphicsItemAnimation's setPosAt() function, and I'm quite puzzled as to the origin of this warning. My code:

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)

What confuses me the most is the fact that this warning goes away when I comment out the last line in the previous section - which from my understanding is related to the QTimer and not the QTimeLine itself. For reference, here is the only other code dealing with the 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()

Any thoughts on the origin of this warning or any possible fixes?

È stato utile?

Soluzione

Your code indentation is a little off (top line should be un-indented?), so I'll summarize your code in words just to confirm I understand what is happening.

You call self.animate(). This method creates an animation, and appends it to a list, self.animations. You iterate over this list and start the animations. You start a timer (with a 1 second timeout) which calls self.animate().

The problem arises because self.animations grows by one element each time you call self.animate(). So the old animation instances are still in the list the next time the method is called. You are iterating over the entire list to start the animations, so you are calling animator.timeLine().start() multiple times on a animation.

Removing the call to the timer prevents the self.animation() method from running more than once, so you don't ever run into the problem when you comment out that line.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top