PyQt - Drawing a Rectangle



A rectangle is a plain geometrical figure that consists of four straight sides and four right angles. The opposite sides are parallel and equal to each other. So it is termed an equiangular quadrilateral.

rectangle

Drawing a Rectangle in PyQt

In PyQt, we draw a rectangle that uses various module such as QtWidgets, QtGui, and QtCore that allow the related functions to draw a rectangle in different shapes. Drawing a rectangle serves various purposes, especially in GUI applications like custom widgets, highlights, bounding boxes, graphics, visualization, and custom drawing.

In PyQt, we draw three different styles of rectangle −

  • Plain Rectangle − A plain Rectangle is a simple shape of a rectangle that follows all its properties.
  • Color Filled Rectangle − The color filled rectangle can be drawn using the methods- setBrush() and drawRect(). The setBrush() method allows to fill the solid color inside a rectangle whereas drawRect() works on coordinates point.
  • Dotted Rectangle − The dot represent the border style of a rectangle. The dotted rectangle can be drawn using various PyQt functions- setStyle(), Qrect(), and drawRect().

In this tutorial, we will show you how to draw a rectangle in PyQt.

Following is the common step to perform the task of plain and color-filled rectangle −

Step 1 − Start with a painter object i.e. QPainter(self).

Step 2 − Next, we perform the method setRenderHints(QtGui.QPainter.RenderHint.Antialiasing) to get the rendering hints flag for the painter object.

Step 3 − Set the pen for line i.e. setpen(QtCore.Qt.GlobalColor.color_name).[Common Step used in both rectangle]. Set the brush style for a line using the setBrush(QtCore.Qt.GlobalColor.color_name) method.[This step is added to fill the color in color filled rectangle.]

Step 4 − Use the method drawRect() to set the coordinates points.

Step 5 − Finally, we get the output of rectangle.

Example 1

Following example to illustrate the rectangle in a plane shape using PyQt. The shape of rectangle can be drawn using the drawRect() method. This method works on four parameters- left, top, right, and bottom.

from PyQt6 import QtWidgets, QtGui, QtCore
from PyQt6.QtGui import QPainter
class MyWidget(QtWidgets.QWidget):
   def paintEvent(self, event):
      painter = QPainter(self)
      painter.setRenderHint(QtGui.QPainter.RenderHint.Antialiasing)
      painter.setPen(QtCore.Qt.GlobalColor.black)
      painter.drawRect(50, 50, 300, 200)

app = QtWidgets.QApplication([])
widget = MyWidget()
widget.show()
app.exec()

Output

The above code produces the output of plain rectangle −

plain rectangle

Example 2

Following example illustrate the shape of color filled rectangle in PyQt.

from PyQt6 import QtWidgets, QtGui, QtCore
from PyQt6.QtGui import QPainter
class MyWidget(QtWidgets.QWidget):
   def paintEvent(self, event):
      painter = QPainter(self)
      painter.setRenderHint(QtGui.QPainter.RenderHint.Antialiasing)
      painter.setPen(QtCore.Qt.GlobalColor.black)
      painter.setBrush(QtCore.Qt.GlobalColor.gray)
      painter.drawRect(50, 50, 300, 200)

app = QtWidgets.QApplication([])
widget = MyWidget()
widget.show()
app.exec()

Output

The above code produces the output of color filled rectangle −

color filled rectangle

Example 3

Building a dotted Rectangle uses various properties of QtGui classes such as QPen(QtCore.Qt.black) to set the border color and set the dash style of border using setStyle() method. Here, we set the rectangle coordinates using Qrect() method. QRect can be designed with a set of left, top, width and height integers. Finally, Qrect passes as a parameter to the method drawRect() to build a rectangle in a dotted shape.

from PyQt6 import QtWidgets, QtGui, QtCore
class MyWidget(QtWidgets.QWidget):
   def paintEvent(self, event):
      painter = QtGui.QPainter(self)
      painter.setRenderHint(QtGui.QPainter.RenderHint.Antialiasing)

      # Set pen properties for a dotted line
      pen = QtGui.QPen(QtCore.Qt.GlobalColor.black)
      pen.setStyle(QtCore.Qt.PenStyle.DashLine)  # Corrected line
      painter.setPen(pen)

      # Set brush for filling the rectangle
      painter.setBrush(QtCore.Qt.GlobalColor.gray)

      rect = QtCore.QRect(50, 50, 300, 200)
      painter.drawRect(rect)

app = QtWidgets.QApplication([])
widget = MyWidget()
widget.show()
app.exec()

Output

The above code produces the output of Dotted Rectangle −

dotted rectangle
Advertisements