Animation of opacity not working
-
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?
-
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)
-
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_())
-
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.