pythonpyqt5qpropertyanimation

How to ignore an event until the other same type event is completed in PyQt


I want you to assume an extremely basic situation where if you hover over a slider, it slightly enlarges and then goes back to its original size when you leave its surface. Here is the code:

def enterEvent(self,event):
    self.anim = QPropertyAnimation(self, b"geometry")
    self.anim.setDuration(200)
    if self.anim.state() == self.anim.State.Stopped:
        rectt = self.geometry()
        self.anim.setStartValue(rectt)
        rectt += QMargins(10,10,10,10)
        self.anim.setEndValue(rectt)
        self.anim.start()

def leaveEvent(self, event):
    if self.anim.state() == self.anim.State.Stopped:
        self.anim.setDirection(self.anim.Backward)
        self.anim.start()
    QSlider.leaveEvent(self,event)

The problem is if you hover over it fast enough, it gets bigger then does not go back to the original size. And, if you keep doing that it continuously enlarges. I do not know why that happens. I already used an if condition to check whether the animation is stopped or running and that statement clearly do not works properly. Is there any way to wait for accepting the next event until the ongoing one is completed.


Solution

  • As @musicamante explained, what I was doing wrong is defining the animation in the enterEvent() which results in a constant animation creation everytime you hover the mouse over the button. Solution is replacing the animation definition outside of the enterEvent() function, to the init function.