QPropertyAnimation 不适用于窗口不透明度 [英] QPropertyAnimation not working with Window Opacity

查看:62
本文介绍了QPropertyAnimation 不适用于窗口不透明度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设置一个新的桌面小部件,让我的工作生活更轻松,并使用 QPropertyAnimation 让它变得漂亮.淡入淡出应用程序似乎不想工作,而且以​​典型的编码器方式,它使我的进度停滞不前.

I'm setting up a new desktop widget to make my life easier at work and using QPropertyAnimation to make it pretty. Fading the app in and out doesn't seem to want to work and in typical coder fashion, it's brought my progress to a standstill.

我正在一个个性化的类中实现 QPropertyAnimation 来让我的生活更轻松,但由于它最初没有工作,我把它带回了类代码,它仍然很顽固.到目前为止,我已经尝试过了.

I'm implementing QPropertyAnimation in a personalised class to make my life easier, but since it's not intially worked I've taken it back to the class code and it's still being pretty stubborn. So far I've tried.

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 个月左右.

No error messages at all it just hides and shows after the animation duration is over. No idea where to look or what to do to get it working. I've only been coding for like 3 months or so.

推荐答案

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

Your code has many errors, for example:

  • 我没看到你在哪里调用 init().
  • 动画是局部变量,会在 show 和 hide 方法完成时移除,这几乎是瞬时的.

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

Instead of changing the opacity directly I will use QGraphicsOpacityEffect, instead of using the show and close method, I will use the showEvent, hideEvent and closeEvent methods.

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_())

这篇关于QPropertyAnimation 不适用于窗口不透明度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆