代码:-
class MasterWindow(QMainWindow):
close_if_clicked=True
# [more code here]
# FIXME - Animation while closing window doesn't work
def mousePressEvent(self, a0: QtGui.QMouseEvent) -> None:
if a0.button()==Qt.MouseButton.LeftButton and self.close_if_clicked:
self.animation = QPropertyAnimation(self, b"windowOpacity")
self.animation.setDuration(1000)
self.animation.setStartValue(1.0)
self.animation.setEndValue(0.0)
self.animation.setEasingCurve(QEasingCurve.Type.InOutQuad)
self.animation.start()
QApplication.exit()
如果我删除
QApplication.exit()
功能动画效果很好,但目前当我点击窗口时它只是在没有动画的情况下退出。
所以我在玩了一会儿之后自己找到了解决方案
# FIXME - Animation while closing window doesn't work
def mousePressEvent(self, a0: QtGui.QMouseEvent) -> None:
if a0.button()==Qt.MouseButton.LeftButton and self.close_if_clicked:
self.animation = QPropertyAnimation(self, b"windowOpacity")
self.animation.setDuration(1000)
self.animation.setStartValue(1.0)
self.animation.setEndValue(0.0)
self.animation.setEasingCurve(QEasingCurve.Type.InOutQuad)
self.animation.finished.connect(QApplication.exit)
self.animation.start()
我的猜测是动画被忽略了,因为应用程序将要退出,通过将它设置为结束函数,我们可以防止它被忽略。