pyqt5qlabel

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


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.


Solution

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