Read Pyqt4 tutorial, get you started Pyqt4 _011

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.