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.

pyqt brushstyle constant

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 −

pyqt brushstyle constant example 2

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 −

pyqt brushstyle constant example 3
Advertisements