在主窗口上使用QProcessAnimation

时间:2016-07-27 06:55:31

标签: python qt pyqt pyside

我在尝试动画QMainWindow时遇到问题。我正在尝试为侧面板制作一个“幻灯片”动画。如果我在“app.exec”之前调用它,但是调用“animate_out”函数似乎没有做任何事情,它工作正常。有什么想法吗?

PS:您可以在底部取消注释代码,以查看我正在寻找的示例。

由于

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

1 个答案:

答案 0 :(得分:1)

问题是animation对象不会比animate_out 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