2016-07-27 4 views
1

Je rencontre un problème lors de l'animation d'une fenêtre QMainWindow. J'essaie de faire une animation "slide" pour un panneau latéral. Cela fonctionne bien si je l'appelle avant le "app.exec" mais en appelant la fonction "animate_out" il semble ne rien faire. Des idées? PS: Vous pouvez annuler le commentaire du code vers le bas pour voir un exemple de ce que je recherche.Utilisation de QProcessAnimation dans la fenêtre principale

Merci

# 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_() 
+0

Avez-vous oublié d'appeler 'animation.start()' dans votre fonction? – Mailerdaimon

+0

HI @Mailerdaimon, merci d'avoir répondu. Oui, j'ai oublié d'ajouter la ligne "animation.start()" dans l'exemple. Cependant, l'ajouter à mon script semble ne faire aucune différence. – DevilWarrior

+0

Essayez de démarrer l'animation après le constucteur. Je ne sais pas si cela fonctionnerait dans le "showEvent" mais vous pourriez essayer. – Mailerdaimon

Répondre

1

Le problème est que l'objet animation ne survivra pas à la portée de la animate_out en fonction. Pour résoudre ce problème, vous devez ajouter l'objet animation en tant que membre à la classe HUB.

Dans mon exemple de code, je divise également la création et la lecture de l'animation en différentes fonctions.

# [...] 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