# FlyingArrowQt.py (c) Kari Laitinen # http://www.naturalprogramming.com # 2009-11-24 File created. # 2009-11-24 Last modification. # This programs shows how the coordinate system can be # re-positioned (i.e."translated"), rotated, and scaled. # In addition, you can see how to draw shapes with the # QPainterPath class. import sys from PyQt4.QtCore import * from PyQt4.QtGui import * class FlyingArrowWindow( QWidget ) : def __init__( self, parent=None ) : QWidget.__init__( self, parent ) self.setGeometry( 100, 100, 800, 500 ) self.setWindowTitle( "DEMONSTRATING 2D GRAPHICS OPERATIONS" ) def paintEvent( self, event ) : # The arrow coordinates are selected so that Point (0, 0) # is at the tip of the arrow, and the arrow points upwards. arrow_shape_coordinates = \ ( QPointF( 0, 0 ), QPointF( 15, 40 ), QPointF( 5, 30 ), QPointF( 5, 120 ), QPointF( 15, 160 ), QPointF( 0, 130 ), QPointF( -15, 160 ), QPointF( -5, 120 ), QPointF( -5, 30 ), QPointF( -15, 40 ) ) flying_arrow = QPainterPath() flying_arrow.moveTo( arrow_shape_coordinates[ 0 ] ) for point_index in range( 1, len( arrow_shape_coordinates ) ) : flying_arrow.lineTo( arrow_shape_coordinates[ point_index ] ) flying_arrow.closeSubpath() painter = QPainter() painter.begin( self ) brush_for_solid_arrows = QBrush( Qt.darkMagenta ) painter.translate( 150, 250 ) # arrow tip to point (150, 250 ) painter.fillPath( flying_arrow, brush_for_solid_arrows ) painter.rotate( 45 ) # 45 degrees clockwise painter.drawPath( flying_arrow ) # draw a hollow arrow painter.translate( 0, -200 ) # flying "up" 200 points painter.fillPath( flying_arrow, brush_for_solid_arrows ) painter.rotate( 45 ) # 45 degrees clockwise painter.translate( 0, -200 ) # flying "up" (i.e. to the right) painter.fillPath( flying_arrow, brush_for_solid_arrows ) painter.translate( 0, -100 ) # flying "up" 100 points painter.rotate( 90 ) # 90 degrees clockwise painter.scale( 1.5, 1.5 ) # magnify everything by 1.5 painter.drawPath( flying_arrow ) # draw a hollow arrow painter.translate( 0, -200 ) # flying "up" (i.e. down) 300 points painter.fillPath( flying_arrow, brush_for_solid_arrows ) # Here is the main program: this_application = QApplication( sys.argv ) application_window = FlyingArrowWindow() application_window.show() this_application.exec_() # NoTES: # Qt color, brush, etc. definitions can be found inside the Qt class # which belongs to the QtCore module: # http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qt.html