PYQT5 uses Qpainter to draw various graphics -2__pyqt5

Source: Internet
Author: User


Http://www.cnblogs.com/hhh5460/p/4281587.html



# file:painter Paths example.py # author:robin # date:2015.2.9 # C + +: Http://doc.qt.io/qt-5/qtwidgets-painting-painterp aths-example.html Import Math from pyqt5.qtcore Import * to Pyqt5.qtgui Import * from pyqt5.qtwidgets Import * class Renderarea (qwidget): Def __init__ (self, Path, Parent=none): Super (Renderarea, self). __init__ (parent) s
        Elf.penwidth = 1 Self.rotationangle = 0 Self.path = Path Self.setbackgroundrole (qpalette.base) Self.setautofillbackground (True) def minimumsizehint (self): return Qsize (M) def sizehint (self) : Return qsize def setfillrule (self, Rule): self.path.setFillRule (rule) self.update
        () def setfillgradient (self, Color1, color2): Self.fillcolor1 = Color1 Self.fillcolor2 = Color2 Self.update () def setpenwidth (self, width): self.penwidth = width self.update () def Setpencol
       or (self, color): Self.pencolor = Color Self.update () def setrotationangle (self, degrees): Self.rotationangle = degree S self.update () def paintevent (Self, event): painter = Qpainter (self) painter.setrenderhint (Q painter.antialiasing) Painter.scale (Self.width ()/100.0, self.height ()/100.0) Painter.translate (50.0, 5 0.0) painter.rotate (-self.rotationangle) painter.translate ( -50.0, -50.0) Painter.setpen (Qpen (self.
        PenColor, Self.penwidth, Qt.solidline, Qt.roundcap, qt.roundjoin)) gradient = qlineargradient (0, 0, 0, 100) Gradient.setcolorat (0.0, Self.fillcolor1) Gradient.setcolorat (1.0, Self.fillcolor2) Painter.setbrush (GRA dient) Painter.drawpath (Self.path) class Mywindow (Qwidget): def __init__ (self): Super (Mywindow, SEL f). __init__ () # Self.setui () # Self. Pi = 3.1415926 # rectangular Path Self.rectpath = Qpainterpath () Self.rectPath.movETo (20.0, 30.0) Self.rectPath.lineTo (80.0, 30.0) Self.rectPath.lineTo (80.0, 70.0) self.rectPath.li
        NeTo (20.0, 70.0) Self.rectPath.closeSubpath () # rounded rectangular path Self.roundrectpath = Qpainterpath () Self.roundRectPath.moveTo (80.0, 35.0) self.roundRectPath.arcTo (70.0, 30.0, 10.0, 10.0, 0.0, 90.0) self.ro Undrectpath.lineto (25.0, 30.0) Self.roundRectPath.arcTo (20.0, 30.0, 10.0, 10.0, 90.0, 90.0) self.roundrect Path.lineto (20.0, 65.0) self.roundRectPath.arcTo (20.0, 60.0, 10.0, 10.0, 180.0, 90.0) SELF.ROUNDRECTPATH.L Ineto (75.0, 70.0) self.roundRectPath.arcTo (70.0, 60.0, 10.0, 10.0, 270.0, 90.0) self.roundRectPath.closeSu Bpath () # ellipse path Self.ellipsepath = Qpainterpath () self.ellipsePath.moveTo (80.0, 50.0) self. Ellipsepath.arcto (20.0, 30.0, 60.0, 40.0, 0.0, 360.0) # pie chart Path Self.piepath = Qpainterpath () self.pi Epath.moveto (50.0, 50.0)
        Self.piePath.arcTo (20.0, 30.0, 60.0, 40.0, 60.0, 240.0) Self.piePath.closeSubpath () # Polygon Path Self.polygonpath = Qpainterpath () self.polygonPath.moveTo (10.0, 80.0) Self.polygonPath.lineTo (20.0, 10. 0) Self.polygonPath.lineTo (80.0, 30.0) Self.polygonPath.lineTo (90.0, 70.0) Self.polygonPath.closeS Ubpath () # combination Path Self.grouppath = Qpainterpath () self.groupPath.moveTo (60.0, 40.0) Self.gro Uppath.arcto (20.0, 20.0, 40.0, 40.0, 0.0, 360.0) self.groupPath.moveTo (40.0, 40.0) Self.groupPath.lineTo (4 0.0, 80.0) self.groupPath.lineTo (80.0, 80.0) self.groupPath.lineTo (80.0, 40.0) Self.groupPath.clos Esubpath () # literal path Self.textpath = Qpainterpath () Self.timesfont = Qfont ("Times", m) self. 
        Timesfont.setstylestrategy (Qfont.forceoutline) Self.textPath.addText (Ten, Self.timesfont, "Qt") # Bezel Path Self.bezierpath = Qpainterpath () self.bezierPath.moveTo () self.bezierPath.cubicTo (0, A/s) Elf.starpath = Qpainterpath () self.starPath.moveTo for I in range (5): Self.starPath.li
        NeTo (0.8 * I * Math.PI), + math.sin (0.8 * I * Math.PI))
        Self.starPath.closeSubpath () Self.renderareas = [] Self.renderAreas.append (Renderarea (Self.rectpath)) Self.renderAreas.append (Renderarea (Self.roundrectpath)) Self.renderAreas.append (Renderarea (Self.ellipsepath 
        )) Self.renderAreas.append (Renderarea (Self.piepath)) Self.renderAreas.append (Renderarea (Self.polygonpath))
        Self.renderAreas.append (Renderarea (Self.grouppath)) Self.renderAreas.append (Renderarea (Self.textpath))

        Self.renderAreas.append (Renderarea (Self.bezierpath)) Self.renderAreas.append (Renderarea (Self.starpath))
# def Setui (self):        Self.fillrulecombobox = Qcombobox () self.fillRuleComboBox.addItem ("Odd even", qt.oddevenfill) Self . Fillrulecombobox.additem ("winding", qt.windingfill) Self.fillrulelabel = Qlabel ("Fill &rule:") self. Fillrulelabel.setbuddy (self.fillrulecombobox) Self.fillcolor1combobox = Qcombobox () Self.populateWithColo RS (Self.fillcolor1combobox) Self.fillColor1ComboBox.setCurrentIndex (Self.fillColor1ComboBox.findText (" 
        Mediumslateblue ")) Self.fillcolor2combobox = Qcombobox () self.populatewithcolors (Self.fillcolor2combobox) Self.fillColor2ComboBox.setCurrentIndex (Self.fillColor2ComboBox.findText ("Cornsilk")) Self.fillgradientl Abel = Qlabel ("&fill gradient:") Self.fillGradientLabel.setBuddy (Self.fillcolor1combobox) Self.fillto Label = Qlabel ("to") Self.fillToLabel.setSizePolicy (qsizepolicy.fixed, qsizepolicy.fixed) SELF.PENWIDTHSP InBox = Qspinbox () self.penwIdthspinbox.setrange (0) Self.penwidthlabel = Qlabel ("&pen Width:") Self.penWidthLabel.setBuddy (SE
        Lf.penwidthspinbox) Self.pencolorcombobox = Qcombobox () self.populatewithcolors (Self.pencolorcombobox)  Self.penColorComboBox.setCurrentIndex (Self.penColorComboBox.findText ("Darkslateblue")) Self.pencolorlabel =  Qlabel ("Pen &color:") Self.penColorLabel.setBuddy (self.pencolorcombobox) Self.rotationanglespinbox =
        Qspinbox () self.rotationAngleSpinBox.setRange (0, 359) self.rotationAngleSpinBox.setWrapping (True) Self.rotationAngleSpinBox.setSuffix ("°") Self.rotationanglelabel = Qlabel ("&rotation Angle:") SELF.R Otationanglelabel.setbuddy (Self.rotationanglespinbox) Self.fillRuleComboBox.activated.connect ( self.fillrulechanged) Self.fillColor1ComboBox.activated.connect (self.fillgradientchanged) self.fillcolor2c OmboBox.activated.connect (self.fillgradientChanged) Self.penColorComboBox.activated.connect (self.pencolorchanged) for it in Self.renderareas: Self.penWidthSpinBox.valueChanged.connect (it.setpenwidth) self.rotationAngleSpinBox.valueChanged.connect
            (it.setrotationangle) Toplayout = Qgridlayout () i = 0 for I, it in Enumerate (Self.renderareas): Toplayout.addwidget (it, I//3, I% 3) Mainlayout = Qgridlayout () mainlayout.addlayout (toplay Out, 0, 0, 1, 4) mainlayout.addwidget (Self.fillrulelabel, 1, 0) mainlayout.addwidget (self.fillrulecombobox , 1, 1, 1, 3) mainlayout.addwidget (Self.fillgradientlabel, 2, 0) Mainlayout.addwidget (self.fillcolor1combo Box, 2, 1) mainlayout.addwidget (Self.filltolabel, 2, 2) Mainlayout.addwidget (Self.fillcolor2combobox, 2, 3
        ) Mainlayout.addwidget (Self.penwidthlabel, 3, 0) mainlayout.addwidget (Self.penwidthspinbox, 3, 1, 1, 3) Mainlayout.addwidgET (Self.pencolorlabel, 4, 0) mainlayout.addwidget (Self.pencolorcombobox, 4, 1, 1, 3) Mainlayout.addwidget ( Self.rotationanglelabel, 5, 0) mainlayout.addwidget (Self.rotationanglespinbox, 5, 1, 1, 3) self.setlayout ( Mainlayout) self.fillrulechanged () self.fillgradientchanged () self.pencolorchanged () Self . Penwidthspinbox.setvalue (2) self.setwindowtitle ("Painter Paths") def fillrulechanged (self): rule = Self.currentitemdata (Self.fillrulecombobox) for it in Self.renderAreas:it.setFillRule (rule) def Fillgradientchanged (self): Color1 = Self.currentitemdata (self.fillcolor1combobox) Color2 = Self.currentite  Mdata (Self.fillcolor2combobox) for it in Self.renderAreas:it.setFillGradient (Color1, Color2) def
            Pencolorchanged (self): color = Self.currentitemdata (Self.pencolorcombobox) for it in Self.renderareas: It.setpencolor(color) @staticmethod def populatewithcolors (comboBox): Colornames = Qcolor.colornames () for name
        In ColorNames:comboBox.addItem (name, Qcolor (name)) @staticmethod def currentitemdata (ComboBox):  Return Combobox.itemdata (Combobox.currentindex (), qt.userrole) if __name__ = = "__main__": Import sys app = Qapplication (sys.argv) win = Mywindow () win.show () Sys.exit (App.exec_ ())

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.