Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Animation in pyqt5

Animation in pyqt5

Scheduled Pinned Locked Moved Solved Qt for Python
qt for python
5 Posts 2 Posters 712 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Steve REDACTED
    wrote on last edited by
    #1

    I made a Farmer John test app to check out the visual performance of a fading dialog (tried it in wxpython -abysmal!)

    #cython: language_level=3
    
    import sys
    from PyQt5.QtWidgets import QApplication, QMainWindow, QDialog
    from PyQt5.QtCore import QPropertyAnimation, QEasingCurve
    
    app=QApplication(sys.argv)
    window=QMainWindow()
    window.setWindowTitle ('Test...')
    animation=QPropertyAnimation(window, bytes('windowOpacity', 'utf8'))
    animation.setDuration (5000)
    animation.setStartValue (1.0)
    animation.setEndValue (0.0)
    animation.finished.connect (window.close)
    animation.start ()
    window.show ()
    app.exec ()
    

    I see it lurch a bit; it is by NO MEANS the smooth transition I have seen with other apps! I tried it as cython, wondering if a slow interpreter might be the culprit; nope. It fades a little; pauses for a couple of seconds; fades a teeny bit more; then finally disappears. Not the gradual transition one would expect.

    Is my code wrong? What's up with this effect??

    JonBJ 1 Reply Last reply
    0
    • S Steve REDACTED

      I made a Farmer John test app to check out the visual performance of a fading dialog (tried it in wxpython -abysmal!)

      #cython: language_level=3
      
      import sys
      from PyQt5.QtWidgets import QApplication, QMainWindow, QDialog
      from PyQt5.QtCore import QPropertyAnimation, QEasingCurve
      
      app=QApplication(sys.argv)
      window=QMainWindow()
      window.setWindowTitle ('Test...')
      animation=QPropertyAnimation(window, bytes('windowOpacity', 'utf8'))
      animation.setDuration (5000)
      animation.setStartValue (1.0)
      animation.setEndValue (0.0)
      animation.finished.connect (window.close)
      animation.start ()
      window.show ()
      app.exec ()
      

      I see it lurch a bit; it is by NO MEANS the smooth transition I have seen with other apps! I tried it as cython, wondering if a slow interpreter might be the culprit; nope. It fades a little; pauses for a couple of seconds; fades a teeny bit more; then finally disappears. Not the gradual transition one would expect.

      Is my code wrong? What's up with this effect??

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @Steve-REDACTED
      I tried your code under Linux. What I see is what I presume you do not like: visually I see maybe 10 "steps" of animation during the 5 seconds. I think you want to see more "steps" than this so that it is "smoother", right?

      First immediately above app.exec() I put in

      animation.valueChanged.connect(lambda value: print(value))
      

      This showed me that that there are lots of calls to change the opacity value during the animation. Good.

      Then I replaced that by:

      animation.valueChanged.connect(lambda value: window.update())
      

      I think you will be happier with this! I get lots of updates, much smoother.

      Clearly the original problem is that the window does not get updated/reshown to the user frequently enough. Now it calls an explicit update() on every value update. I am not sure what the requirements are, how often the animation updates without you calling update() yourself, but clearly in this case it is not enough without you giving it a helping hand.

      1 Reply Last reply
      0
      • JonBJ JonB referenced this topic on
      • S Offline
        S Offline
        Steve REDACTED
        wrote on last edited by
        #3

        Thanks a zillion; I don't get why many of the online examples omitted that. A window that fades out is not an uncommon thing!

        1 Reply Last reply
        0
        • S Steve REDACTED has marked this topic as solved on
        • S Offline
          S Offline
          Steve REDACTED
          wrote on last edited by
          #4

          There's just one little frustrating quirk now: after the window fades out, as QT exits the window reappears for an instant, then disappears. I figured this made some sense since it was a return to state of the window's default opacity of 1; so I did a window.setWindowOpacity (0.0) [right after creating it with window=QMainWindow()] Unfortunately it still does it.

          Going to do some searches now to see if anyone else worked around this...

          1 Reply Last reply
          0
          • S Offline
            S Offline
            Steve REDACTED
            wrote on last edited by Steve REDACTED
            #5

            Got the window flashing removed when the app exits. I changed

            animation.finished.connect (window.close)
            

            to call a function of mine that does the window.close (). When I added a time.sleep (1) after the window.close, it stops that behavior. So it appears qt needs more time when it is cleaning up the window, and when you prematurely interrupt it (with an application exit...), the window repaints for an instant.

            AND: you don't need to set an initial opacity of 0...

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved