Read Pyqt4 tutorial, get you started Pyqt4 _013

Source: Internet
Author: User
Tags drawtext emit

Have you ever looked at an application and thought about how a particular GUI item was generated ? That's about every programmer. Then you can see a series of widgets provided by your favorite GUI library, but you can't find it. Toolkits typically provide only the most common window components, such as buttons, text components, sliders, and so on. No toolkit is available to provide all possible components.

There are actually two kinds of toolkits, lightweight and heavyweight . The FLTK Toolkit is a lightweight toolkit that only provides very basic components and assumes that programmers can create more complex components on their own. PYQT4 is heavyweight , it has many window components, but does not provide a very specialized window component. A speedometer window component, for example, is used to measure the capacity of a burned CD (found in Nero). Also does not contain commonly used charts.

Programmers must create these window components themselves, which are created using the drawing tools provided by the toolkit. There are two ways to modify or enhance an existing component, or to create from scratch .

Burn window Components

This is the window component we see in Nero, k3b, or other CD-burning software.

#!/usr/bin/python#-*-coding:utf-8-*-"""Zetcode PyQt4 Tutorialin This program, we create a customburning widget.author:Jan Bodnarwebsite:zetcode.comlast EDI Ted:december"""ImportSYS fromPyQt4ImportQtgui, QtcoreclassBurningwidget (qtgui.qwidget):def __init__(self): Super (Burningwidget, self).__init__() Self.initui ()defInitui (self): Self.setminimumsize (1, 30) Self.value= 75Self.num= [75, 150, 225, 300, 375, 450, 525, 600, 675] Self.connect (self, qtcore.signal ("updateburningwidget (int)"), Self.setvalue)defSetValue (self, value): Self.value=valuedefpaintevent (Self, e): QP=Qtgui.qpainter () qp.begin (self) self.drawwidget (QP) qp.end ()defDrawwidget (Self, QP): Font= Qtgui.qfont ('Serif', 7, QtGui.QFont.Light) qp.setfont (font) size=self.size () W=size.width () H=size.height () step= Int (Round (w/10.0)) till= Int (((w/750.0) *self.value)) full= Int (((w/750.0) * 700))        ifSelf.value >= 700: Qp.setpen (Qtgui.qcolor (255, 255, 255)) Qp.setbrush (Qtgui.qcolor (255, 255, 184)) qp.drawrect (0, 0, full, h) Qp.setpen (Qtgui.qcolor (255, 175, 175)) Qp.setbrush (Qtgui.qcolor (255, 175, 175)) Qp.drawrect (full, 0, till-Full , h)Else: Qp.setpen (Qtgui.qcolor (255, 255, 255)) Qp.setbrush (Qtgui.qcolor (255, 255, 184)) qp.drawrect (0, 0, till, h) pen= Qtgui.qpen (Qtgui.qcolor (20, 20, 20), 1, QtCore.Qt.SolidLine) qp.setpen (pen) qp.setbrush (QtCore.Qt.NoBrush) qp.drawrect (0, 0, W-1, H-1) J=0 forIinchRange (step, 10*step, Step): Qp.drawline (i, 0, I,5) Metrics=qp.fontmetrics () FW=metrics.width (str (SELF.NUM[J)) Qp.drawtext (i-FW/2, H/2, str (SELF.NUM[J])) J= j + 1classExample (qtgui.qwidget):def __init__(self): Super (Example, self).__init__() Self.initui ()defInitui (self): slider=Qtgui.qslider (QtCore.Qt.Horizontal, self) slider.setfocuspolicy (QtCore.Qt.NoFocus) Slider.setrange (1, 750) Slider.setvalue (75) Slider.setgeometry (30, 40, 150, 30) Self.wid=Burningwidget () self.connect (slider, qtcore.signal ('valuechanged (int)'), Self.changevalue) Hbox=qtgui.qhboxlayout () hbox.addwidget (self.wid) VBox=qtgui.qvboxlayout () Vbox.addstretch (1) vbox.addlayout (Hbox) self.setlayout (vbox) self.setgeometry (300, 300, 300, 220) Self.setwindowtitle ('Burning')    defChangeValue (self, Value): Self.wid.emit (Qtcore.signal ("updateburningwidget (int)"), value) Self.wid.repaint ()defMain (): App=qtgui.qapplication (SYS.ARGV) ex=Example () ex.show () app.exec_ ( )if __name__=='__main__': Main ()

In this example, we have another Qslider and a custom window component, which is used to control the custom window component. The window component graphically displays the total amount of media and free space available. The minimum value for a custom car port is 1, and the maximum value is 750. If it reaches 700, it will start to draw red. Usually represents a super-tick.

Burned parts are usually placed in the lower part of the form, using a qhboxlayout and a qvboxlayout to achieve the goal.

class Burningwidget (qtgui.qwidget):     def __init__ (self):        super (Burningwidget, self). __init__ ()

The burn window component is based on the Qlabel window component.

Self.setminimumsize (1, 30)

Modify the window component minimum (height), the default value is a bit small for us.

Font = Qtgui.qfont ('Serif', 7, QtGui.QFont.Light) paint.setfont (font)

Using a font that is smaller than the default value is more appropriate for our needs.

Size == = = Int (round (w/10.0= Int ((w/750.0) *= Int (((w/750.0) * 700))

We draw the window component dynamically, and when the window becomes larger, the burning window component becomes larger, and vice versa. That's why we have to calculate the dimensions of the parts to our custom parts. The till parameter determines all dimensions that are drawn. value is obtained from the slider, which is the ratio of the entire area. The full parameter determines the point at which we start to draw red. Note using floating-point arithmetic, in order to achieve higher precision.

The actual drawing consists of three steps. We draw the yellow or red and yellow rectangles, then draw the vertical lines, divide the parts into multiple parts, and finally draw numbers to indicate the capacity of the media.

Metrics == metrics.width (str (SELF.NUM[J)) Qp.drawtext (i-FW/2, H/2, str (SELF.NUM[J]))

We use the self-measure to draw the text, we must know the width of the text so that the text can be drawn in the middle of the vertical line.

def ChangeValue (self, value):    self.wid.emit (qtcore.signal ("updateburningwidget (int) " ), value)    self.wid.repaint ()

When we move the slider, the changevalue () method is called, in which we send a custom updateburningwidget (int) signal and the corresponding parameter, which is the current value of the slider. This value is later used to calculate the capacity of the burned part and draw it.

In this section of the PyQt4 tutorial, we created a custom window component.

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/4625304.html

Read Pyqt4 tutorial, get you started Pyqt4 _013

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.