
- PyQt - Home
- PyQt - Introduction
- PyQt - Environment
- PyQt - Hello World
- PyQt - Major Classes
- PyQt - Using Qt Designer
- PyQt - Meta Objects
- PyQt Signals & Slots
- PyQt - Signals and Slots
- PyQt - Support and Signals
- PyQt - Unbound and Bound Signals
- PyQt - New Signals with PyQtSignal
- PyQt - Connecting, Disconnecting, & Emitting Signals
- PyQt - Slot decorator
- PyQt - Slot Connection
- PyQt Layouts
- PyQt - Layout Management
- PyQt - QBoxLayout
- PyQt - QGridLayout
- PyQt - QFormLayout
- PyQt - QHBoxLayout
- PyQt - QVBoxLayout
- PyQt - QStackedLayout
- PyQt - QGraphicsGridLayout
- PyQt - QGraphicsAnchorLayout
- PyQt - QGraphicsLayout
- PyQt - QGraphicsLinearLayout
- PyQt Basic Widgets
- PyQt - Basic Widgets
- PyQt - Qlabel Widget
- PyQt - QLineEdit Widget
- PyQt - QPushButton Widget
- PyQt - QRadioButton Widget
- PyQt - QCheckBox Widget
- PyQt - QComboBox Widget
- PyQt - QSpinBox Widget
- PyQt - QMessageBox
- PyQt - QDialogButtonBox Widget
- PyQt - QFontComboBox Widget
- PyQt - QDoubleSpinBox Widget
- PyQt - QToolBox Widget
- PyQt - QMenuBar, QMenu & Qaction Widgets
- PyQt - QToolTip
- PyQt - QInputDialog Widget
- PyQt - QFontDialog Widget
- PyQt - QDialog Widget
- PyQt - QFileDialog Widget
- PyQt - QTab Widget
- PyQt - QSplitter Widget
- PyQt - QDock Widget
- PyQt - QStatusBar Widget
- PyQt - QTabBar
- PyQt - QList Widget
- PyQt - QScrollBar Widget
- PyQt - QProgressBar
- PyQt - QCalendar Widget
- PyQt - QMessageBox Widget
- PyQt - QPlainTextEdit
- PyQt - QDateEdit
- PyQt - QDateTimeEdit
- PyQt - QTimeEdit
- PyQt - QTextEdit
- PyQt - QTextBrowser
- PyQt - QScrollArea
- PyQt - Drag and Drop
- PyQt - Multiple Document Interface
- PyQt - QDialog Class
- PyQt Views
- PyQt - QColumnView
- PyQt - QTableView
- PyQt Drawing API
- PyQt - Drawing API
- PyQt - Drawing a Line
- PyQt - Drawing a Rectangle
- PyQt - Drawing a Triangle
- PyQt - Drawing a Circle
- PyQt - Drawing a Ellipse
- PyQt - Drawing a Polygon
- PyQt - Geometric Transformation
- PyQt - Drawing Effect
- PyQt Groups
- PyQt - QButtonGroup
- PyQt - QGroupBox
- PyQt Effects
- PyQt - Effects
- PyQt - Opacity Effect
- PyQt - QGraphicsBlur Effect
- PyQt - QGraphicsColorize Effect
- PyQt - QGraphicsDropShadow Effect
- PyQt Events
- PyQt - Event Handling
- PyQt - Drag & Drop Events
- PyQt - File Open Event
- PyQt - Action Event
- PyQt - Hide Event
- PyQt - Resize Event
- PyQt Database
- PyQt - Database Handling
- PyQt Essentials
- PyQt - BrushStyle Constants
- PyQt - QClipboard
- PyQt - QPixmap Class
- PyQt Useful Resources
- PyQt - Quick Guide
- PyQt - Useful Resources
- PyQt - Discussion
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.

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 −

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 −

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 −
