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 of opacity not working
Forum Updated to NodeBB v4.3 + New Features

Animation of opacity not working

Scheduled Pinned Locked Moved Solved Qt for Python
4 Posts 2 Posters 1.3k Views 2 Watching
  • 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.
  • M Offline
    M Offline
    mrigeoy
    wrote on last edited by
    #1

    I am trying to run a group animation to simultaneously move a window to the top left of the screen and reduce its opacity.

    The movement is good but I see no change to the opacity.

    If the code is edited so that fadeEffect acts on the button instead of the mainWindow then both movement and animation work as expected.

    import sys
    from PyQt5 import QtCore, QtWidgets
    
    app = QtWidgets.QApplication(sys.argv)
    
    mainWindow = QtWidgets.QWidget()
    mainWindow.setStyleSheet('background-color: grey')
    button = QtWidgets.QPushButton('Push me')
    layout = QtWidgets.QHBoxLayout(mainWindow)
    layout.addWidget(button)
    mainWindow.show()
    
    topLeft = mainWindow.frameGeometry().topLeft()
    size = mainWindow.frameSize()
    moveAnim = QtCore.QPropertyAnimation(mainWindow, b'geometry')
    moveAnim.setStartValue(QtCore.QRect(topLeft, size))
    moveAnim.setEndValue(QtCore.QRect(QtCore.QPoint(40, 40), size))
    moveAnim.setDuration(2000)
    
    fadeEffect = QtWidgets.QGraphicsOpacityEffect(mainWindow)
    mainWindow.setGraphicsEffect(fadeEffect)
    fadeAnim = QtCore.QPropertyAnimation(fadeEffect, b'opacity')
    fadeAnim.setStartValue(1)
    fadeAnim.setEndValue(0.3)
    fadeAnim.setDuration(2000)
    
    group = QtCore.QParallelAnimationGroup()
    group.addAnimation(moveAnim)
    group.addAnimation(fadeAnim)
    group.start()
    
    exit(app.exec_())
    
    

    How do I find a way to fade the mainWindow widget?

    1 Reply Last reply
    0
    • JoeCFDJ Offline
      JoeCFDJ Offline
      JoeCFD
      wrote on last edited by JoeCFD
      #2

      replace mainWindow.setGraphicsEffect(fadeEffect) with fadeAnim.setTargetObject( fadeEffect ), but after fadeAnim = QtCore.QPropertyAnimation(fadeEffect, b'opacity');

      fadeEffect = QtWidgets.QGraphicsOpacityEffect(mainWindow)
      fadeAnim = QtCore.QPropertyAnimation(fadeEffect, b'opacity')
      fadeAnim.setTargetObject( fadeEffect )
      fadeAnim.setStartValue(1)
      fadeAnim.setEndValue(0.3)
      fadeAnim.setDuration(2000)
      
      1 Reply Last reply
      0
      • M Offline
        M Offline
        mrigeoy
        wrote on last edited by
        #3

        Thanks for your response @JoeCFD but unfortunately your suggested changes didn't work for me.

        I did find a solution though.

        After rereading the documentation I realised that 'opacity' is not a property of QWidget Class but 'windowOpacity' is.

        So with a bit of help from a response on this Qt Forum post from @Devopia53 I came up with this simpler code.

        import sys
        from PyQt5 import QtCore, QtWidgets
        
        app = QtWidgets.QApplication(sys.argv)
        
        mainWindow = QtWidgets.QWidget()
        mainWindow.setStyleSheet('background-color: grey')
        button = QtWidgets.QPushButton('Push me')
        layout = QtWidgets.QHBoxLayout(mainWindow)
        layout.addWidget(button)
        mainWindow.show()
        
        topLeft = mainWindow.frameGeometry().topLeft()
        size = mainWindow.frameSize()
        moveAnim = QtCore.QPropertyAnimation(mainWindow, b'geometry')
        moveAnim.setStartValue(QtCore.QRect(topLeft, size))
        moveAnim.setEndValue(QtCore.QRect(QtCore.QPoint(40, 40), size))
        moveAnim.setDuration(2000)
        
        fadeAnim = QtCore.QPropertyAnimation(mainWindow, b'windowOpacity')
        fadeAnim.setStartValue(1)
        fadeAnim.setEndValue(0.3)
        fadeAnim.setDuration(2000)
        
        group = QtCore.QParallelAnimationGroup()
        group.addAnimation(moveAnim)
        group.addAnimation(fadeAnim)
        group.start()
        
        exit(app.exec_())
        
        
        1 Reply Last reply
        0
        • JoeCFDJ Offline
          JoeCFDJ Offline
          JoeCFD
          wrote on last edited by JoeCFD
          #4

          After rereading the documentation I realised that 'opacity' is not a property of QWidget Class but 'windowOpacity' is
          it may not be true.
          I did the following:
          m_opacityEffect = new QGraphicsOpacityEffect( this );
          m_animatedWidget->setGraphicsEffect( m_opacityEffect );

          I have a generic animation launcher for geometry and opacity which works with any specific qwidget.

          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