Qt 启动时设置窗口动画
我试图在启动时设置窗口的动画,但它似乎不起作用,我已经编写了下面的代码Qt 启动时设置窗口动画,qt,pyqt,pyqt4,pyside,Qt,Pyqt,Pyqt4,Pyside,我试图在启动时设置窗口的动画,但它似乎不起作用,我已经编写了下面的代码 from PyQt4 import QtCore,QtGui import sys class AnimatedWindow(QtGui.QMainWindow): """docstring for AnimatedWindow""" def __init__(self): super(AnimatedWindow, self).__init__() animation =
from PyQt4 import QtCore,QtGui
import sys
class AnimatedWindow(QtGui.QMainWindow):
"""docstring for AnimatedWindow"""
def __init__(self):
super(AnimatedWindow, self).__init__()
animation = QtCore.QPropertyAnimation(self, "geometry")
animation.setDuration(10000);
animation.setStartValue(QtCore.QRect(0, 0, 100, 30));
animation.setEndValue(QtCore.QRect(250, 250, 100, 30));
animation.start();
if __name__ == "__main__":
application = QtGui.QApplication(sys.argv)
main = AnimatedWindow()
main.show()
sys.exit(application.exec_())
此代码的问题是,当您创建
QPropertyAnimation
的对象时,它会在animation.start()
语句之后被python垃圾收集器销毁,因为animation
变量是局部变量,因此不会发生动画。要解决此问题,需要将动画
作为成员变量(self.animation
)
以下是运行良好的更新代码:
from PyQt4 import QtCore,QtGui
import sys
class AnimatedWindow(QtGui.QMainWindow):
"""docstring for AnimatedWindow"""
def __init__(self):
super(AnimatedWindow, self).__init__()
self.animation = QtCore.QPropertyAnimation(self, "geometry")
self.animation.setDuration(1000);
self.animation.setStartValue(QtCore.QRect(50, 50, 100, 30));
self.animation.setEndValue(QtCore.QRect(250, 250, 500, 530));
self.animation.start();
if __name__ == "__main__":
application = QtGui.QApplication(sys.argv)
main = AnimatedWindow()
main.show()
sys.exit(application.exec_())
QPropertyAnimation
类在Qt 4.6
及更高版本中受支持,您使用的是什么版本?我将通过给出问题的实际原因更新此答案