
- 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 - BrushStyle Constants
BrushStyle constants in PyQt define different patterns and styles used for filling areas, such as rectangles, ellipses, or polygons. These patterns add visual appeal and distinction to the elements of a PyQt application.
PyQt offers a range of BrushStyle constants, each representing a unique pattern or style. Some of the commonly used BrushStyle constants include −
- SolidPattern − Fills the area with a solid color.
- Dense1Pattern, Dense2Pattern, ..., Dense7Pattern − Fills the area with dense patterns of varying densities.
- HorPattern, VerPattern, CrossPattern, BDiagPattern, FDiagPattern − Fills the area with horizontal, vertical, cross, backward diagonal, or forward diagonal patterns, respectively.
- LinearGradientPattern − Fills the area with a linear gradient.
- RadialGradientPattern − Fills the area with a radial gradient.
- ConicalGradientPattern − Fills the area with a conical gradient.
- ConicalGradientPattern − Fills the area with a conical gradient.
Predefined QColor Styles
Qt.NoBrush | No brush pattern |
Qt.SolidPattern | Uniform color |
Qt.Dense1Pattern | Extremely dense brush pattern |
Qt.HorPattern | Horizontal lines |
Qt.VerPattern | Vertical lines |
Qt.CrossPattern | Crossing horizontal and vertical lines |
Qt.BDiagPattern | Backward diagonal lines |
Qt.FDiagPattern | Forward diagonal lines |
Qt.DiagCrossPattern | Crossing diagonal lines |
Predefined QColor Objects
Qt.white |
Qt.black |
Qt.red |
Qt.darkRed |
Qt.green |
Qt.darkGreen |
Qt.blue |
Qt.cyan |
Qt.magenta |
Qt.yellow |
Qt.darkYellow |
Qt.gray |
Custom color can be chosen by specifying RGB or CMYK or HSV values.
Example 1:Filling a Rectangle with Solid Color
In this example, we'll create a PyQt application that draws a rectangle filled with a solid color.
In the below example, in the MainWindow class, we override the paintEvent method to perform custom painting. We create a QPainter object and set the brush to a solid red color using QBrush and Qt.SolidPattern. We draw a rectangle using the drawRect method of the QPainter object.
import sys from PyQt5.QtWidgets import QApplication, QMainWindow from PyQt5.QtGui import QPainter, QColor, QBrush from PyQt5.QtCore import Qt class MainWindow(QMainWindow): def __init__(self): super().__init__() def paintEvent(self, event): painter = QPainter(self) painter.setBrush(QBrush(Qt.red, Qt.SolidPattern)) painter.drawRect(100, 100, 200, 100) if __name__ == "__main__": app = QApplication(sys.argv) window = MainWindow() window.setGeometry(100, 100, 400, 300) window.show() sys.exit(app.exec_())
Output
The above code produces the following output. A PyQt window will open, displaying a rectangle filled with a solid red color.

Example 2: Filling a Rectangle with Dense Pattern
In the below example, we create a QBrush object with a dense green pattern (Qt.Dense4Pattern). We set the brush of the QPainter object to the created brush. Finally, we draw a rectangle using the drawRect method.
import sys from PyQt5.QtWidgets import QApplication, QMainWindow from PyQt5.QtGui import QPainter, QColor, QBrush from PyQt5.QtCore import Qt class MainWindow(QMainWindow): def __init__(self): super().__init__() def paintEvent(self, event): painter = QPainter(self) brush = QBrush(Qt.green, Qt.Dense4Pattern) painter.setBrush(brush) painter.drawRect(100, 100, 200, 100) if __name__ == "__main__": app = QApplication(sys.argv) window = MainWindow() window.setGeometry(100, 100, 400, 300) window.show() sys.exit(app.exec_())
Output
The above code produces the following result −

Example 3: Filling a Rectangle with LinearGradientPattern
In this example, we'll create a PyQt application that draws a rectangle filled with a linear gradient.
In the below example, we override the paintEvent method for custom painting. We create a QLinearGradient object defining the gradient's start and end points. We set the colors of the gradient using setColorAt method. We create a QBrush object with the linear gradient. Finally, we draw a rectangle using the drawRect method.
import sys from PyQt5.QtWidgets import QApplication, QMainWindow from PyQt5.QtGui import QPainter, QBrush, QLinearGradient from PyQt5.QtCore import Qt class MainWindow(QMainWindow): def __init__(self): super().__init__() def paintEvent(self, event): painter = QPainter(self) gradient = QLinearGradient(0, 0, 400, 300) gradient.setColorAt(0, Qt.blue) gradient.setColorAt(1, Qt.yellow) brush = QBrush(gradient) painter.setBrush(brush) painter.drawRect(100, 100, 200, 100) if __name__ == "__main__": app = QApplication(sys.argv) window = MainWindow() window.setGeometry(100, 100, 400, 300) window.show() sys.exit(app.exec_())
Output
The above code produces the follwing result −
