在 main window 上使用 QProcessAnimation
Use QProcessAnimation on main window
我在尝试为 QMainWindow 设置动画时遇到问题。我正在尝试为侧面板制作 "slide" 动画。如果我在 "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_()
问题是 animation
对象不会超过 animate_out
out 函数的范围。
要解决此问题,您必须将 animation
对象作为成员添加到 HUB
class.
在我的示例代码中,我还将动画的创建和播放拆分为不同的函数。
# [...] 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
我在尝试为 QMainWindow 设置动画时遇到问题。我正在尝试为侧面板制作 "slide" 动画。如果我在 "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_()
问题是 animation
对象不会超过 animate_out
out 函数的范围。
要解决此问题,您必须将 animation
对象作为成员添加到 HUB
class.
在我的示例代码中,我还将动画的创建和播放拆分为不同的函数。
# [...] 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