QPropertyAnimation不适用于窗口不透明度

时间:2019-09-06 20:11:10

标签: python python-3.x pyside2 qpropertyanimation

我正在设置一个新的桌面小部件,以使工作更轻松,并使用QPropertyAnimation使其美观。将应用程序渐入渐出似乎并不想工作,并且以典型的编码器方式运行,这使我的进步陷入了停滞。

我正在一个个性化的类中实现QPropertyAnimation,以使我的生活更轻松,但是由于它最初没有起作用,所以我将其带回到类代码中,并且仍然很顽固。到目前为止,我已经尝试过了。

class widget(QWidget):

def init(self):
   self.setSize(QSize(300, 300))
   self.setWindowOpacity(1)
   self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint)
   self.setAttribute(Qt.WA_TranslucentBackground)

def paintEvent(self, event):
   s = self.size()
   qp = QPainter()
   qp.begin(self)
   qp.setRenderHint(QPainter.Antialiasing, True)
   qp.setBrush(QColor().fromRgb(2,106,194))
   qp.setPen(QColor().fromRgb(2,106,194))
   qp.drawRoundRect(QRect(0,0, 300, 300), 16, 8)
   qp.end()

def show(self):
   self.superShow()
   a = QPropertyAnimation(self, "windowOpacity")
   a.setDuration(500)
   a.setStartValue(1)
   a.setEndValue(0)
   a.start()

def hide(self):
   a = QPropertyAnimation(self, "windowOpacity")
   a.setDuration(500)
   a.setStartValue(0)
   a.setEndValue(1)
   a.finished.connect(self.superHide)
   a.start()

def superShow(self):
   super(widget, self).show()

def superHide(self):
   super(widget, self).hide()

没有任何错误消息,只是在动画持续时间结束后隐藏并显示。不知道在哪里看或如何使它工作。我只编码了大约3个月左右。

1 个答案:

答案 0 :(得分:1)

您的代码有很多错误,例如:

  • 我看不到您在哪里调用init()。
  • 动画是局部变量,当show和hide方法完成时,它们将被删除,这几乎是瞬时的。

我将使用QGraphicsOpacityEffect而不是直接更改不透明度,而不是使用show and close方法,而是使用showEvent,hideEvent和closeEvent方法。

import sys
from PySide2.QtCore import QEasingCurve, QEventLoop, QPropertyAnimation, QRect, QSize, Qt
from PySide2.QtGui import QColor, QPainter
from PySide2.QtWidgets import QAction, QApplication, QGraphicsOpacityEffect, QWidget


class Widget(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.resize(QSize(300, 300))
        # self.setWindowOpacity(1)
        self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint)
        self.setAttribute(Qt.WA_TranslucentBackground)
        self.setContextMenuPolicy(Qt.ActionsContextMenu)

        quit_action = QAction(self.tr("E&xit"), self)
        quit_action.setShortcut(self.tr("Ctrl+Q"))
        quit_action.triggered.connect(self.close)
        self.addAction(quit_action)

        effect = QGraphicsOpacityEffect(self, opacity=1.0)
        self.setGraphicsEffect(effect)
        self._animation = QPropertyAnimation(
            self,
            propertyName=b"opacity",
            targetObject=effect,
            duration=500,
            startValue=0.0,
            endValue=1.0,
        )

    def paintEvent(self, event):
        qp = QPainter(self)
        qp.setRenderHint(QPainter.Antialiasing, True)
        qp.setBrush(QColor().fromRgb(2, 106, 194))
        qp.setPen(QColor().fromRgb(2, 106, 194))
        qp.drawRoundedRect(QRect(0, 0, 300, 300), 16, 8)

    def fade_in(self):
        self._animation.setDirection(QPropertyAnimation.Forward)
        self._animation.start()

    def fade_out(self):
        loop = QEventLoop()
        self._animation.finished.connect(loop.quit)
        self._animation.setDirection(QPropertyAnimation.Backward)
        self._animation.start()
        loop.exec_()

    def showEvent(self, event):
        super().showEvent(event)
        self.fade_in()

    def closeEvent(self, event):
        # fade out
        self.fade_out()
        super().closeEvent(event)

    def hideEvent(self, event):
        # fade out
        self.fade_out()
        super().hideEvent(event)


if __name__ == "__main__":
    import sys

    app = QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())