In this Python article we are going to learn about Drawing Polygon in Pyside2 with QPainter class,
so the QPainter class performs low-level painting on widgets and other paint devices.
Also you can check more Python GUI Development tutorials with different libraries.
1: PyQt5 GUI Development Tutorials
2: TKinter GUI Development Tutorials
3: wxPython GUI Development Tutorials
4: Kivy GUI Development Tutorials
Learn How to Draw Rectangle in Pyside2 with QPainter class
What is QPainter Class ?
According to Qt For Python Documentation the QPainter provides highly optimized functions
to do most of the drawing GUI programs require. It can draw everything from simple lines to
complex shapes like pies and chords. It can also draw aligned text and pixmaps. Normally, it
draws in a “natural” coordinate system, but it can also do view and world transformation.
QPainter can operate on any object that inherits the QPaintDevice class.
What is Polygon ?
According to Wikipedia In geometry, a polygon is a plane figure that is described by a finite
number of straight line segments connected to form a closed polygonal chain or polygonal
circuit. The solid plane region, the bounding circuit, or the two together, may be called a polygon.
So now this is the complete Source code for Python Drawing Polygon in Pyside2 with QPainter
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
from PySide2.QtWidgets import QApplication, QWidget from PySide2.QtGui import QPainter, QPen, QBrush, QPolygon from PySide2.QtCore import Qt, QPoint import sys class Window(QWidget): def __init__(self): super().__init__() self.setWindowTitle("Pyside2 Drawing Polygon") self.setGeometry(300,200,700,500) self.show() def paintEvent(self, e): painter = QPainter(self) painter.setPen(QPen(Qt.black, 5, Qt.SolidLine)) # painter.setBrush(QBrush(Qt.red, Qt.SolidPattern)) painter.setBrush(QBrush(Qt.red, Qt.VerPattern)) points = QPolygon([ QPoint(10, 10), QPoint(10, 100), QPoint(100, 10), QPoint(100, 100) ]) painter.drawPolygon(points) app = QApplication(sys.argv) window = Window() sys.exit(app.exec_()) |
These are the imports that we need for this article.
1 2 3 4 |
from PySide2.QtWidgets import QApplication, QWidget from PySide2.QtGui import QPainter, QPen, QBrush, QPolygon from PySide2.QtCore import Qt, QPoint import sys |
In this line of code we have set our window title, also we are going to set the geometry for
the window, you can use setGeometry() method and it takes some parameters like x, y position
also width and height.
1 2 |
self.setWindowTitle("Pyside2 Drawing Polygon") self.setGeometry(300,200,700,500) |
When you want to draw using QPainter class, you need to add paintEvent() method, it is a
built in method for QPainter class and it is used for drawing shapes in Pyside2. you can see in
this method first we have created the object of QPainter class, after that we have set the Pen
and Brush style for our QPainter class. for drawing of Polygon we need some points , so for this,
first we need to create some points using QPolygon class. The QPolygon class provides a vector
of points using integer precision. after that we draw our polygon using drawPolygon() method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
def paintEvent(self, e): painter = QPainter(self) painter.setPen(QPen(Qt.black, 5, Qt.SolidLine)) # painter.setBrush(QBrush(Qt.red, Qt.SolidPattern)) painter.setBrush(QBrush(Qt.red, Qt.VerPattern)) points = QPolygon([ QPoint(10, 10), QPoint(10, 100), QPoint(100, 10), QPoint(100, 100) ]) painter.drawPolygon(points) |
For every Pyside2 application we need to create the object of QApplication.
1 |
app = QApplication(sys.argv) |
And this is the starting point of the loop.
1 |
sys.exit(app.exec_()) |
So run the complete code and this will be the result.
Subscribe and Get Free Video Courses & Articles in your Email