Search code examples
pythonpyqt6qpropertyanimation

pyqt6 animation doesnt work for decreasing width


I have struggle with animation. It works first time to increase width but with else statements it doesnt' decrease size.

def slideLeftMenu(self):
        self.animation=QPropertyAnimation(self.ui.LeftMenuContainer,b'minimumWidth')
        self.animation.setDuration(250)
       
        width=self.ui.LeftMenuContainer.width()
      
    
       
        if width==50:
            
            self.ui.mainBodyContainer.move(100,0)
            self.animation.setStartValue(50)
            self.animation.setEndValue(100)
            self.animation.start()
            
        else:
             self.ui.mainBodyContainer.move(50,0)
             self.animation.setStartValue(100)
             self.animation.setEndValue(50)
             self.animation.setEasingCurve(QtCore.QEasingCurve.Type.InOutQuart)
             self.animation.start()

Solution

  • Try this:

    def slideLeftMenu(self):
        # GET WIDTH
        width = self.ui.LeftMenuContainer.width()
        widthExtended = 50
    
        # SET MAX WIDTH
        if width == 50:
            widthExtended = 100
    
        # ANIMATION
        self.animation = QtCore.QPropertyAnimation(self.frame_left_menu, b"minimumWidth")
        self.animation.setDuration(250)
        self.animation.setStartValue(50)
        self.animation.setEndValue(widthExtended)
        self.animation.setEasingCurve(QtCore.QEasingCurve.Type.InOutQuart)
        self.animation.start()