PyQt/GraphicsView_-_SimpleAnimation
This wiki is in the process of being archived due to lack of usage and the resources necessary to serve it — predominately to bots, crawlers, and LLM companies. Edits are discouraged.
Pages are preserved as they were at the time of archival. For current information, please visit python.org.
If a change to this archive is absolutely needed, requests can be made via the infrastructure@python.org mailing list.
Graphics View - Simple Animation
The example below presents how to use QGraphicsView along with QGraphicsItem, QGraphicsItemAnimation and QTimeLine to construct a very simple animation. If you have any questions please do not hesitate to ask me directly.
1
2
3 import sys
4 from PyQt4 import QtGui, QtCore
5
6 class MyView(QtGui.QGraphicsView):
7 def __init__(self):
8 QtGui.QGraphicsView.__init__(self)
9
10 self.scene = QtGui.QGraphicsScene(self)
11 self.item = QtGui.QGraphicsEllipseItem(-20, -10, 40, 20)
12 self.scene.addItem(self.item)
13 self.setScene(self.scene)
14
15
16
17 self.tl = QtCore.QTimeLine(1000)
18 self.tl.setFrameRange(0, 100)
19 self.a = QtGui.QGraphicsItemAnimation()
20 self.a.setItem(self.item)
21 self.a.setTimeLine(self.tl)
22
23
24
25
26 self.a.setPosAt(0, QtCore.QPointF(0, -10))
27 self.a.setRotationAt(1, 360)
28
29 self.tl.start()
30
31 if __name__ == '__main__':
32 app = QtGui.QApplication(sys.argv)
33 view = MyView()
34 view.show()
35 sys.exit(app.exec_())
(WinXP, Py2.6, PyQt4.4.4)