PyQt5 uses QPainter to plot various graphics, and pyqt5qpainter
In this example, I have been doing this for several days:
1) the source code of C ++ on the official website is rewritten to the code of PyQt5. Many details will not be converted.
2) The PyQt instance on the Internet cannot run at all.
I have filled in countless pitfalls and combined them to finally complete a drawing. This process also has a lot of new knowledge points
[Knowledge point]
1. Use of multiple points
Poitns = [QPoint (10, 80), QPoint (20, 10), QPoint (80, 30), QPoint (90, 70)]
See:
# Define multiple points = [QPoint (10, 80), QPoint (20, 10), QPoint (80, 30), QPoint (90, 70)] #=== if you use points directly, an error is returned! ========== #... Elif self. shape = self. points: painter. drawPoints (points) elif self. shape = self. polyline: painter. drawPolyline (points) elif self. shape = self. polygon: painter. drawPolygon (points, 4 )#... #=== it is correct to wrap points with QPolygon! ========== #... Elif self. shape = self. points: painter. drawPoints (QPolygon (points) elif self. shape = self. polyline: painter. drawPolyline (QPolygon (points) elif self. shape = self. polygon: painter. drawPolygon (QPolygon (points), 4 )#...
2. display the QWidget component in the QDialog form
【]
[Resource]
Http://files.jb51.net/file_images/article/201710/brick.png
Http://files.jb51.net/file_images/article/201710/qt-logo.png
[Code]
Import sysfrom PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidgets import * class StockDialog (QWidget): def _ init _ (self, parent = None): super (StockDialog, self ). _ init _ (parent) self. setWindowTitle ("using QPainter to draw various graphics") mainSplitter = QSplitter (Qt. horizontal) mainSplitter. setOpaqueResize (True) frame = QFrame (mainSplitter) mainLayout = QGridLayout (frame) # mainLayout. setMargin (10) mainLa Yout. setSpacing (6) label1 = QLabel ("shape:") label2 = QLabel ("paint width:") label3 = QLabel ("paint color :") label4 = QLabel ("paint brush style:") label5 = QLabel ("paint brush top:") label6 = QLabel ("paint brush connection point:") label7 = QLabel ("paint brush style :") label8 = QLabel ("paint color:") self. shapeComboBox = QComboBox () self. shapeComboBox. addItem ("Line", "Line") self. shapeComboBox. addItem ("Rectangle", "Rectangle") self. shapeComboBox. addItem ('rounded Rectangle ', 'rounded Rectangle') s Elf. shapeComboBox. addItem ('ellipse', 'ellipse') self. shapeComboBox. addItem ('pie', 'pie') self. shapeComboBox. addItem ('chord ', 'chord') self. shapeComboBox. addItem ('path', 'path') self. shapeComboBox. addItem ('polygon', 'polygon') self. shapeComboBox. addItem ('line', 'line') self. shapeComboBox. addItem ('arc', 'arc') self. shapeComboBox. addItem ('points', 'points') self. shapeComboBox. addItem ('text', 'text') s Elf. shapeComboBox. addItem ('pixmap', 'pixmap') self. widthSpinBox = QSpinBox () self. widthSpinBox. setRange (0, 20) self. penColorFrame = QFrame () self. penColorFrame. setAutoFillBackground (True) self. penColorFrame. setPalette (QPalette (Qt. blue) self. penColorPushButton = QPushButton ("change") self. penStyleComboBox = QComboBox () self. penStyleComboBox. addItem ("Solid", Qt. solidLine) self. penStyleComboBox. addItem ('da Sh', Qt. dashLine) self. penStyleComboBox. addItem ('dot ', Qt. dotLine) self. penStyleComboBox. addItem ('dash Dot ', Qt. dashDotLine) self. penStyleComboBox. addItem ('dash Dot ', Qt. dashDotDotLine) self. penStyleComboBox. addItem ('none', Qt. noPen) self. penCapComboBox = QComboBox () self. penCapComboBox. addItem ("Flat", Qt. flatCap) self. penCapComboBox. addItem ('square ', Qt. squareCap) self. penCapComboBox. addItem (' Round ', Qt. roundCap) self. penJoinComboBox = QComboBox () self. penJoinComboBox. addItem ("Miter", Qt. miterJoin) self. penJoinComboBox. addItem ('bebel ', Qt. bevelJoin) self. penJoinComboBox. addItem ('round ', Qt. roundJoin) self. brushStyleComboBox = QComboBox () self. brushStyleComboBox. addItem ("Linear Gradient", Qt. linearGradientPattern) self. brushStyleComboBox. addItem ('radial gradient', Qt. radialGradientPattern) Self. brushStyleComboBox. addItem ('conical gradient', Qt. conicalGradientPattern) self. brushStyleComboBox. addItem ('texture ', Qt. texturePattern) self. brushStyleComboBox. addItem ('solid', Qt. solidPattern) self. brushStyleComboBox. addItem ('horizontal ', Qt. horPattern) self. brushStyleComboBox. addItem ('signature', Qt. verPattern) self. brushStyleComboBox. addItem ('cross ', Qt. crossPattern) self. brushStyleComboB Ox. addItem ('backward diagonal', Qt. BDiagPattern) self. brushStyleComboBox. addItem ('forward diagonal', Qt. FDiagPattern) self. brushStyleComboBox. addItem ('diagonal Cross ', Qt. diagCrossPattern) self. brushStyleComboBox. addItem ('dense 1', Qt. dense1Pattern) self. brushStyleComboBox. addItem ('dense 2', Qt. dense2Pattern) self. brushStyleComboBox. addItem ('dense 3', Qt. dense3Pattern) self. brushStyleComboBox. addI Tem ('dense 4', Qt. dense4Pattern) self. brushStyleComboBox. addItem ('dense 5', Qt. dense5Pattern) self. brushStyleComboBox. addItem ('dense 6', Qt. dense6Pattern) self. brushStyleComboBox. addItem ('dense 7', Qt. dense7Pattern) self. brushStyleComboBox. addItem ('none', Qt. noBrush) self. brushColorFrame = QFrame () self. brushColorFrame. setAutoFillBackground (True) self. brushColorFrame. setPalette (QPalette (Qt. green )) Self. brushColorPushButton = QPushButton ("change") labelCol = 0 contentCol = 1 # create a layout mainLayout. addWidget (label1, 1, labelCol) mainLayout. addWidget (self. shapeComboBox, 1, contentCol) mainLayout. addWidget (label2, 2, labelCol) mainLayout. addWidget (self. widthSpinBox, 2, contentCol) mainLayout. addWidget (label3, 4, labelCol) mainLayout. addWidget (self. penColorFrame, 4, contentCol) mainLayout. addWidget (self. penColorPushButt On, 4, 3) mainLayout. addWidget (label4, 6, labelCol) mainLayout. addWidget (self. penStyleComboBox, 6, contentCol) mainLayout. addWidget (label5, 8, labelCol) mainLayout. addWidget (self. penCapComboBox, 8, contentCol) mainLayout. addWidget (label6, 10, labelCol) mainLayout. addWidget (self. penJoinComboBox, 10, contentCol) mainLayout. addWidget (label7, 12, labelCol) mainLayout. addWidget (self. brushStyleComboBox, 12, contentCol) m AinLayout. addWidget (label8, 14, labelCol) mainLayout. addWidget (self. brushColorFrame, 14, contentCol) mainLayout. addWidget (self. brushColorPushButton, 14,3) mainSplitter1 = QSplitter (Qt. horizontal) mainSplitter1.setOpaqueResize (True) stack1 = QStackedWidget () stack1.setFrameStyle (QFrame. panel | QFrame. raised) self. area = PaintArea () stack1.addWidget (self. area) frame1 = QFrame (mainSplitter1) mainLayout1 = Q VBoxLayout (frame1) # mainLayout1.setMargin (10) mainLayout1.setSpacing (6) mainLayout1.addWidget (stack1) layout = QGridLayout (self) layout. addWidget (mainSplitter1, 0, 0) layout. addWidget (mainSplitter, 0, 1) self. setLayout (layout) # signal and slot functions self. shapeComboBox. activated. connect (self. slotShape) self. widthSpinBox. valueChanged. connect (self. slotPenWidth) self. penColorPushButton. clicked. connect (self. slotPenColor) Self. penStyleComboBox. activated. connect (self. slotPenStyle) self. penCapComboBox. activated. connect (self. slotPenCap) self. penJoinComboBox. activated. connect (self. slotPenJoin) self. brushStyleComboBox. activated. connect (self. slotBrush) self. brushColorPushButton. clicked. connect (self. slotBrushColor) self. slotShape (self. shapeComboBox. currentIndex () self. slotPenWidth (self. widthSpinBox. value () self. slotBru Sh (self. brushStyleComboBox. currentIndex () def slotShape (self, value): shape = self. area. shape [value] self. area. setShape (shape) def slotPenWidth (self, value): color = self. penColorFrame. palette (). color (QPalette. window) style = Qt. penStyle (self. penStyleComboBox. itemData (self. penStyleComboBox. currentIndex (), Qt. userRole) cap = Qt. penCapStyle (self. penCapComboBox. itemData (self. penCapComboBox. currentInde X (), Qt. userRole) join = Qt. penJoinStyle (self. penJoinComboBox. itemData (self. penJoinComboBox. currentIndex (), Qt. userRole) self. area. setPen (QPen (color, value, style, cap, join) def slotPenStyle (self, value): self. slotPenWidth (value) def slotPenCap (self, value): self. slotPenWidth (value) def slotPenJoin (self, value): self. slotPenWidth (value) def slotPenColor (self): color = QColorDialog. getColor (Qt. blue) sel F. penColorFrame. setPalette (QPalette (color) self. area. setPen (QPen (color) def slotBrushColor (self): color = QColorDialog. getColor (Qt. blue) self. brushColorFrame. setPalette (QPalette (color) self. slotBrush (self. brushStyleComboBox. currentIndex () def slotBrush (self, value): color = self. brushColorFrame. palette (). color (QPalette. window) style = Qt. brushStyle (self. brushStyleComboBox. itemData (value, Qt. user Role) if (style = Qt. linearGradientPattern): linearGradient = QLinearGradient (0,0, 400,400) linearGradient. setColorAt (0.0, Qt. white) linearGradient. setColorAt (0.2, color) linearGradient. setColorAt (1.0, Qt. black) self. area. setBrush (linearGradient) elif style = Qt. radialGradientPattern: radialGradient = QRadialGradient (200,200, 80, 70, 70); radialGradient. setColorAt (0.0, Qt. white) radialGradient. setCo LorAt (0.2, Qt. green) radialGradient. setColorAt (1.0, Qt. black) self. area. setBrush (radialGradient) elif (style = Qt. conicalGradientPattern): conicalGradient = QConicalGradient (200,200, 30) conicalGradient. setColorAt (0.0, Qt. white) conicalGradient. setColorAt (0.2, color) conicalGradient. setColorAt (1.0, Qt. black) self. area. setBrush (conicalGradient) elif (style = Qt. texturePattern): self. area. setBrush (QBrus H (QPixmap ("images/brick.png") else: self. area. setBrush (QBrush (color, style) class PaintArea (QWidget): def _ init _ (self): super (PaintArea, self ). _ init _ () self. shape = ["Line", "Rectangle", "Rounded Rectangle", "Ellipse", "Pie", 'chord "," Path "," Polygon "," Polyline ", "Arc", "Points", "Text", "Pixmap"] self. setPalette (QPalette (Qt. white) self. setAutoFillBackground (True) self. setMinimumSize (400,400) Self. pen = QPen () self. brush = QBrush () def setShape (self, s): self. shape = s self. update () def setPen (self, p): self. pen = p self. update () def setBrush (self, B): self. brush = B self. update () def paintEvent (self, QPaintEvent): p = QPainter (self) p. setPen (self. pen) p. setBrush (self. brush) rect = QRect (50,100,300,200) points = [QPoint (150,100), QPoint (300,150), QPoint (350,250), QPoint (100,300)] startAngle = 30*16 spanAngle = 120*16 path = QPainterPath (); path. addRect (150,150,100,100) path. moveTo (100,100) path. cubicTo (300,100,200,200,300,300) path. cubicTo (100,300,200,200,100,100) if self. shape = "Line": p. drawLine (rect. topLeft (), rect. bottomRight () elif self. shape = "Rectangle": p. drawRect (rect) elif self. shape = 'rounded rectang': p. drawRoundedRect (rect, 25, 25, Qt. relativeSize) elif self. Shape = "Ellipse": p. drawEllipse (rect) elif self. shape = "Polygon": p. drawPolygon (QPolygon (points), Qt. windingFill) elif self. shape = "Polyline": p. drawPolyline (QPolygon (points) elif self. shape = "Points": p. drawPoints (QPolygon (points) elif self. shape = "Pie": p. drawPie (rect, startAngle, spanAngle) elif self. shape = "Arc": p. drawArc (rect, startAngle, spanAngle) elif self. shape = "Chord": p. DrawChord (rect, startAngle, spanAngle) elif self. shape = "Path": p. drawPath (path) elif self. shape = "Text": p. drawText (rect, Qt. alignCenter, "Hello Qt! ") Elif self. shape = "Pixmap": p. drawPixmap (150,150, QPixmap ("images/qt-logo.png") if _ name __= = '_ main _': app = QApplication (sys. argv) form = StockDialog () form. show () app.exe c _()
The above example of using QPainter to draw various graphics in PyQt5 is all the content that I have shared with you. I hope you can give us a reference and support the help house.