2016-07-27 37 views
1

我在試圖爲QMainWindow設置動畫時遇到問題。我試圖爲側面板製作「幻燈片」動畫。如果我在「app.exec」之前調用它,然而調用「animate_out」函數它似乎沒有做任何事情,它工作正常。有任何想法嗎? PS:您可以取消對註釋底部的代碼進行註釋,以查看我正在尋找的示例。在主窗口上使用QProcessAnimation

感謝

# 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

你忘了在函數中調用'animation.start()'嗎? – Mailerdaimon

+0

你好@Mailerdaimon,謝謝你的回覆。是的,我忘了在示例中添加「animation.start()」行。但是將它添加到我的腳本似乎沒有任何區別。 – DevilWarrior

+0

嘗試在構造函數之後開始動畫。我不知道它是否會在「showEvent」中起作用,但您可以嘗試一下。 – Mailerdaimon

回答

1

的問題是animation對象不會活得比的animate_out輸出功能的範圍。 要解決此問題,您必須將animation對象作爲成員添加到HUB類。

在我的示例代碼中,我還將創建和播放動畫分爲不同的功能。

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