PyQt - Drawing a Polygon



A polygon is consider as multiple geometrical shapes which has aleast 3 sides, angles, or typically five or more than its. It is two-dimensional closed form shape of straight line. The example of polygon such as triangle, quadilateral, pentagon, hexagon, etc.

drawing polygon

Drawing a Polygon in PyQt

In PyQt, a Polygon can be drawn either by the methods createPoly() or QPolygon(). These methods are called using drawPolygon(). The drawPolygon method is referenced from the class QPainter, i.e. used to draw a polygon in a QPainter device or widget.

Example

In this example, we follow the method named createPoly() to draw a polygon using PyQt.

import sys
import math
from PyQt6 import QtCore, QtGui, QtWidgets
class MyWidget(QtWidgets.QWidget):
   def __init__(self, parent=None):
      QtWidgets.QWidget.__init__(self, parent)
      # set lineColor
      self.pen = QtGui.QPen(QtGui.QColor(0, 0, 0))
      # set line width
      self.pen.setWidth(3)
      # set fillColor
      self.brush = QtGui.QBrush(QtGui.QColor(255, 255, 255, 255))
      # The createPoly parameters- n point, radius, angle of first point
      self.polygon = self.createPoly(8,140,0)
   def createPoly(self, n, r, s):
      polygon = QtGui.QPolygonF()
      # angle as per step
      w = 360/n
      # add the points to polygon
      for i in range(n):
         t = w * i + s
         x = r * math.cos(math.radians(t))
         y = r * math.sin(math.radians(t))
         polygon.append(QtCore.QPointF(self.width() / 2 + x, self.height() / 2 + y))
      return polygon

   def paintEvent(self, event):
      painter = QtGui.QPainter(self)
      painter.setPen(QtCore.Qt.GlobalColor.green)
      painter.setBrush(QtCore.Qt.GlobalColor.gray)
      painter.drawPolygon(self.polygon)

app = QtWidgets.QApplication(sys.argv)
widget = MyWidget()
widget.show()
sys.exit(app.exec())

Output

The above code produces the following output −

drawing polygon example one

Drawing a Polygon(triangle) in PyQt

As we earlier discussed about polygons that consider a minimum of 3 sides and angles. So, below the example illustrates tringle using various methods such as QPolygon(), QPoint(), and setGeometry().

The built-in function of QPolygon and Qpoint classes −

  • QPolygon() − The class QPolygon itself a built-in function QPolygon() that store the list of point using integer precision.
  • Qpoint() − The Qpoint class perform the same functionality w.r.t QPolygon. The class QPoint works on two parameters- x coordinate and y coordinate.

Note that, QPoint() can be applied multiple times to the method QPolygon based on polygon shape.

Example

Following example demonstrate the code snippet of polygon using PyQt.

import sys
from PyQt6.QtGui import QPainter, QPolygon, QColor
from PyQt6.QtWidgets import QWidget, QApplication
from PyQt6.QtCore import QPoint
class Example(QWidget):
   def __init__(self):
      super().__init__()
      self.setGeometry(400, 400, 450, 300)

   def paintEvent(self, event):
      qp = QPainter(self)
      qp.setPen(QColor(0, 0, 0))
      qp.setBrush(QColor(255, 155, 156))
      points = QPolygon([QPoint(60, 60), QPoint(160, 60), QPoint(110, 160)])
      qp.drawPolygon(points)

if __name__ == '__main__':
   app = QApplication(sys.argv)
   ex = Example()
   ex.show()
   sys.exit(app.exec())

Output

The above code produces the following output −

drawing polygon example two
Advertisements