You can use a drawing when we want to change or enhance an existing window component , or when you are ready to create a custom window component from scratch . We draw by using the drawing API provided by the PYQT4 Toolkit.
The drawing is carried out in the paintevent () square method. The drawing code is between the begin () and end () of the qpainter object .
Draw text
We start by drawing some Unicode text in the client area of the window.
#!/usr/bin/python#-*-coding:utf-8-*-#drawtext.pyImportSYS fromPyQt4ImportQtgui, QtcoreclassExample (qtgui.qwidget):def __init__(self): Super (Example, self).__init__() Self.initui ()defInitui (self): Self.setgeometry (300, 300, 250, 150) Self.setwindowtitle ('Draw Text') Self.text= u'\u041b\u0435\u0432 \u041d\u0438\u043a\u043e\u043b\u0430 \u0435\u0432\u0438\u0447 \u0422\u043e\u043b\u0441\ u0442\u043e\u0439: \ n \u0410\u043d\u043d\u0430 \u041a\u0430\u0440\u0435\u043d\u0438\u043d\u0430' defpaintevent (Self, event): QP=Qtgui.qpainter () qp.begin (self) Self.drawtext (event, QP) qp.end ()defDrawText (Self, event, QP): Qp.setpen (Qtgui.qcolor (168, 34, 3)) Qp.setfont (Qtgui.qfont ('Decorative', 10) ) Qp.drawtext (Event.rect (), QtCore.Qt.AlignCenter, Self.text) app=qtgui.qapplication (SYS.ARGV) ex=Example () ex.show () app.exec_ ( )
In our example, we draw some Cyrillic text, Wenzheng and vertically aligned.
def paintevent (Self, event):
Paint in the drawing event.
QP = Qtgui.qpainter () qp.begin (self) Self.drawtext (event, QP) qp.end ()
The Qpainter class is responsible for all low-level paintings. All drawing methods are between the begin () and End () methods. The actual drawing here is the proxy to the DrawText () method.
Paint.setpen (Qtgui.qcolor (168, 3)) Paint.setfont (Qtgui.qfont ('decorative' ) , 10))
Here we define the brushes and the fonts that are used to draw the text.
Paint.drawtext (Event.rect (), QtCore.Qt.AlignCenter, Self.text)
The DrawText () method draws the text on the window, and the rect () method that draws the event returns the rectangle that needs to be updated.
Draw points
A point is the simplest drawing object that can be drawn, and is a small area of the window.
#!/usr/bin/python#-*-coding:utf-8-*-#points.pyImportsys, Random fromPyQt4ImportQtgui, QtcoreclassExample (qtgui.qwidget):def __init__(self): Super (Example, self).__init__() self.setgeometry (300, 300, 250, 150) Self.setwindowtitle ('Points') defpaintevent (Self, e): QP=Qtgui.qpainter () qp.begin (self) self.drawpoints (QP) qp.end ()defdrawpoints (Self, QP): Qp.setpen (QtCore.Qt.red) Size=self.size () forIinchRange (1000): x= Random.randint (1, Size.width ()-1) y= Random.randint (1, Size.Height ()-1) qp.drawpoint (x, y) app=qtgui.qapplication (SYS.ARGV) ex=Example () ex.show () app.exec_ ( )
In this example, we randomly plot 1000 red dots in the client area.
Paint.setpen (QtCore.Qt.red)
Use the predefined color constants to set the brush to red.
Size = Self.size ()
Each time we zoom the window, it will produce a drawing event. The size of the window is obtained through the size () method, and the window size is used to distribute the points to the client area of the window.
Paint.drawpoint (x, y)
Use the drawpoint () method to draw a point.
This site article is for baby bus SD. Team Original, reproduced must be clearly noted: (the author's official website: Baby bus )
Reprinted from "Baby bus Superdo Team" original link: http://www.cnblogs.com/superdo/p/4593894.html
Read Pyqt4 tutorial, get you started Pyqt4 _011