DevilWarrior
DevilWarrior

Reputation: 459

Use QProcessAnimation on main window

I'm having an issue trying to animate a QMainWindow. I'm trying to make a "slide" animation for a side panel. It works fine if I call it before the "app.exec" however calling the "animate_out" function it seems not to do anything. Any ideas?

PS: You can un-comment the code towards the bottom to see an example of what I'm looking for.

Thanks

# PYQT IMPORTS
from PyQt4 import QtCore, QtGui
import sys
import UI_HUB


# MAIN HUB CLASS
class HUB(QtGui.QMainWindow, UI_HUB.Ui_HUB):

    def __init__(self):
        super(self.__class__, self).__init__()
        self.setupUi(self)
        self.setCentralWidget(self._Widget)
        self.setWindowTitle('HUB - 0.0')
        self._Widget.installEventFilter(self)
        self.setWindowFlags(QtCore.Qt.FramelessWindowHint | QtCore.Qt.WindowStaysOnTopHint)
        self.set_size()

        self.animate_out()

    def set_size(self):
        # Finds available and total screen resolution
        resolution_availabe = QtGui.QDesktopWidget().availableGeometry()
        ava_height = resolution_availabe.height()
        self.resize(380, ava_height)

    def animate_out(self):
        animation = QtCore.QPropertyAnimation(self, "pos")
        animation.setDuration(400)
        animation.setStartValue(QtCore.QPoint(1920, 22))
        animation.setEndValue(QtCore.QPoint(1541, 22))
        animation.setEasingCurve(QtCore.QEasingCurve.OutCubic)
        animation.start()


if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    form = HUB()
    form.show()
    form.raise_()
    form.activateWindow()

    # Doing the animation here works just fine
    # animation = QtCore.QPropertyAnimation(form, "pos")
    # animation.setDuration(400)
    # animation.setStartValue(QtCore.QPoint(1920, 22))
    # animation.setEndValue(QtCore.QPoint(1541, 22))
    # animation.setEasingCurve(QtCore.QEasingCurve.OutCubic)
    # animation.start()

    app.exec_()

Upvotes: 1

Views: 662

Answers (1)

Mailerdaimon
Mailerdaimon

Reputation: 6080

The problem is that the animation Object will not outlive the scope of the animate_out out function. To solve this you have to add the animation Object as a member to the HUB class.

In my Example code I also split the creation and playing of the animation into different functions.

# [...] skipped
class HUB(QtGui.QMainWindow, UI_HUB.Ui_HUB):

    def __init__(self):
        # [...] skipped 
        self.create_animations() # see code below
        self.animate_out()

    def set_size(self):
        # [...] skipped

    def create_animations(self):
        # set up the animation object
        self.animation = QtCore.QPropertyAnimation(self, "pos")
        self.animation.setDuration(400)
        self.animation.setStartValue(QtCore.QPoint(1920, 22))
        self.animation.setEndValue(QtCore.QPoint(1541, 22))
        self.animation.setEasingCurve(QtCore.QEasingCurve.OutCubic)

    def animate_out(self)
        # use the animation object
        self.animation.start()
# [...] skipped

Upvotes: 1

Related Questions