The pyqt6 animation does not manage to work properly WHY?

122 Views Asked by At

I did a toggle menu when I pressed the button it will expand(Increase the height) and shrink(Decrease the height). That is what I expected. It turns out that it fine for expanding the menu but not with shrinking.

I set QFrame with minimum: W 201 H 0 maximum: W 201 H 0

def toggleMenu(self,maxHeight,enable): 
    if enable : 
      #get height 
      height = self.ui.LeftMenu.height() 
      print(height)
      maxExtend = maxHeight 
      standard = 0

      #set height

      if height == 0 : 
        heightExtended = maxExtend 
      else: 
        heightExtended = standard 

    
    

      # Animation  
      print(heightExtended)
      self.animation = QPropertyAnimation(self.ui.LeftMenu, b"minimumHeight") 
      self.animation.setDuration(200) 
      self.animation.setStartValue(height) 
      self.animation.setEndValue(heightExtended) 
      self.animation.setEasingCurve(QEasingCurve.Type.InOutQuart)
      self.animation.start() 

enter image description here

1

There are 1 best solutions below

0
user16618398 On

Here what I got and it works!!! Hope it will help the other for similar problem ;-;

  def toggleMenu(self, maxHeight, enable):
      if enable:
          # Get current height
          currentHeight = self.ui.LeftMenu.height()
          
          # Define the start and end values for the animation
          startValue = currentHeight
          endValue = maxHeight if currentHeight == 0 else 0
          
          # Create the animation
          animation = QVariantAnimation(self)
          animation.setStartValue(startValue)
          animation.setEndValue(endValue)
          animation.setDuration(200)
          animation.setEasingCurve(QEasingCurve.InOutQuart)
          
          # Define the function to be called on each animation frame
          def onValueChanged(value):
              self.ui.LeftMenu.setFixedHeight(value)

          
          # Connect the animation to the function
          animation.valueChanged.connect(onValueChanged)
          
          # Start the animation
          animation.start()