如何同时在两个不同的QLabel上运行两个独立的QGraphicsOpacityEffect。

huangapple go评论42阅读模式
英文:

How to run multiple two separate QGraphicsOpacityEffect on two separate QLabels simultaneously

问题

假设您有两个QLabel和一个QWindow,label1label2,您想对它们同时运行QGraphicsOpacityEffect。这两个效果必须同时开始,同时播放,但总持续时间可以不同。

我进行了很多谷歌搜索。找不到任何解释,只找到一个关于在同一个部件上执行两个单独动画的解释 - 这不是我要做的。

任何帮助将不胜感激 xoxo

我尝试使用了QThread、QThreadPool和QParallelAnimationGroup,但似乎都不能满足我的需求,也许我只是经验不足。

英文:

Say you have two QLabels a QWindow, label1 and label2, and you want to run a QGraphicsOpacityEffect on both of them. The two effects must start at the same time, play simultaneously, but last differing lengths of time in total.

I have done so many Google searches. Cannot find any explanation, only one on doing two separate animations on the SAME widget - NOT what I am trying to do.

Any help would be greatly appreciated xoxo

I tried using QThread, QThreadPool, and QParallelAnimationGroup, but none of these seemed to be able to suffice, but maybe I'm just inexperienced.

答案1

得分: 0

I'm a muppet. Certified Doo-Doo head. Anyway I figured it out.

Basically, instead of redefining a single self.animation value each time I launch a new animation (which stops the old one, silly goose), I have an array of animations that I had new ones to. Here's the MRE I made to do it:

from PyQt5.QtCore import QPropertyAnimation, QAbstractAnimation
import sys


class Window(QMainWindow):
    def __init__(self):
        super(Window, self).__init__()
        self.setWindowTitle('Window')
        self.animations = []

        self.label1 = QLabel('Label 1!', self)
        self.label1.move(10, 10)
        self.label2 = QLabel('Label 2!', self)
        self.label2 move(10, 110)

        self.showMaximized()
        self.fadeOutSequence()

    def animateWidget(self, widget, duration):
        effect = QGraphicsOpacityEffect()
        self.animations.append(QPropertyAnimation(effect, b'opacity'))
        widget.setGraphicsEffect(effect)
        self.animations[len(self.animations)-1].setDuration(duration)
        self.animations[len(self.animations)-1].setStartValue(0)
        self.animations[len(self.animations)-1].setEndValue(1)
        self.animations[len(self.animations)-1].start(QAbstractAnimation.DeleteWhenStopped)

    def fadeOutSequence(self):
        self.animateWidget(self.label1, 4000)
        self.animateWidget(self.label2, 8000)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Window()
    sys.exit(app.exec_())
英文:

I'm a muppet. Certified Doo-Doo head. Anyway I figured it out.

Basically, instead of redefining a single self.animation value each time I launch a new animation (which stops the old one, silly goose), I have an array of animations that I had new ones to. Here's the MRE I made to do it:

from PyQt5.QtCore import QPropertyAnimation, QAbstractAnimation
import sys


class Window(QMainWindow):
    def __init__(self):
        super(Window, self).__init__()
        self.setWindowTitle('Window')
        self.animations = []

        self.label1 = QLabel('Label 1!', self)
        self.label1.move(10, 10)
        self.label2 = QLabel('Label 2!', self)
        self.label2.move(10, 110)

        self.showMaximized()
        self.fadeOutSequence()

    def animateWidget(self, widget, duration):
        effect = QGraphicsOpacityEffect()
        self.animations.append(QPropertyAnimation(effect, b'opacity'))
        widget.setGraphicsEffect(effect)
        self.animations[len(self.animations)-1].setDuration(duration)
        self.animations[len(self.animations)-1].setStartValue(0)
        self.animations[len(self.animations)-1].setEndValue(1)
        self.animations[len(self.animations)-1].start(QAbstractAnimation.DeleteWhenStopped)

    def fadeOutSequence(self):
        self.animateWidget(self.label1, 4000)
        self.animateWidget(self.label2, 8000)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Window()
    sys.exit(app.exec_())

huangapple
  • 本文由 发表于 2023年5月10日 18:39:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76217416.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定