PyQt - QGraphicsBlur Effect



Imagine we are developing a photo editor application using PyQt, and we want to implement a feature of bluring part of selective image. To achieve this, we use the class QGraphicsBlur in PyQt. This effect allows user to insert a blur on specific items in a QGraphicsScene. This class provides a surface management for large number of two-dimensional graphical items. It is used together with QGraphicsView for visualizing graphical items, such as lines, rectangles, text, etc.

The item's location in the scene is determined by its scene position concerning the scene coordinates. In addition, the scene bounding of any item is used by the QGraphicsScene to identify which areas of the scene have been modified.

The common description list of following classes which used to build a blurred layout item −

  • QGraphicsEffect − This is base class for all graphical effect.
  • QGraphicsBlurEffect − This class can be used to set the blurred effect to an items.
  • QGraphicsScene − This class is used for visualizing items.

Following is the common step to perform the task of blur effect in different items −

  • Step 1 − Mention the classes from the library of PyQt
  • Step 2 − Use the specific method which you want to draw/create on the window such as QVBoxLayout(), QGraphicsRectItem , setGeometry(), etc.
  • Step 3 − Set the method QGraphicsBlurEffect() to respective variable.
  • Step 4 − Adjust the percentage of blur using setBlurRadius() method.
  • Step 5 − Use setGraphicsEffect() to call the method QGraphicsBlurEffect().
  • Step 6 − Display the result.

Example 1

In the following example, we create a button to the window using the methods − QVBoxLayout() and QPushButton(). The class QGraphicsBlurEffect itself performs a built-in function to set the blurring effect to a specified item and with the use of setBlurRadius(), the percentage of blur-effect occurred on a clickable button.

from PyQt6.QtWidgets import QGraphicsEffect, QGraphicsBlurEffect, QPushButton, 
QApplication, QVBoxLayout, QWidget
import sys
class MainWindow(QWidget):
   def __init__(self):
      super().__init__()

      layout = QVBoxLayout()

      button = QPushButton("Click Here")
      blur_effect = QGraphicsBlurEffect()
      blur_effect.setBlurRadius(3)
      button.setGraphicsEffect(blur_effect)

      layout.addWidget(button)
      self.setLayout(layout)

if __name__ == "__main__":
   app = QApplication(sys.argv)
   window = MainWindow()
   window.show()
   sys.exit(app.exec())

Output

The above code produces the following output −

qgraphicsblur effect

Example 2

In the following example, we draw a rectangle using the class QGraphicsRectItem which uses its function to build the rectangle in a window. Then we apply the blur effect using QGraphicsBlurEffect() and the percentage of blur is measured using setBlurRadius().

import sys
from PyQt6.QtWidgets import QApplication, QGraphicsView, QGraphicsScene, 
QGraphicsBlurEffect, QGraphicsRectItem
def main():
   app = QApplication(sys.argv)
   view = QGraphicsView()
   scene = QGraphicsScene()
   view.setScene(scene)

   # Create a rectangle item
   rect_item = QGraphicsRectItem(0, 0, 200, 100)
   scene.addItem(rect_item)

   # Apply blur effect to the rectangle
   blur_effect = QGraphicsBlurEffect()
   # Adjust the blur radius as needed
   blur_effect.setBlurRadius(10)
   rect_item.setGraphicsEffect(blur_effect)

   view.show()
   sys.exit(app.exec())

if __name__ == "__main__":
   main()

Output

The above code produces the following output −

qgraphicsblur_effect_one

Example 3

In the following example, we use QLabel() to set the dummy text on the window. To make it blur it uses the function QGraphicsBlurEffect(). The blurred text appeared in the center of a window using setCentralWidget() and displayed the result.

import sys
from PyQt6.QtWidgets import QApplication, QMainWindow, 
QLabel, QVBoxLayout, QWidget, QGraphicsBlurEffect

class BlurredTextApp(QMainWindow):
   def __init__(self):
      super().__init__()
      self.setWindowTitle("Blurred Text Example")
      self.setGeometry(100, 100, 400, 200)

      central_widget = QWidget()
      layout = QVBoxLayout(central_widget)

      # Create a QLabel with blurred text
      label = QLabel("Welcome to Tutorialspoint")
      blur_effect = QGraphicsBlurEffect()
      # Adjust the blur radius as needed
      blur_effect.setBlurRadius(2)
      label.setGraphicsEffect(blur_effect)

      layout.addWidget(label)
      self.setCentralWidget(central_widget)

if __name__ == "__main__":
   app = QApplication(sys.argv)
   window = BlurredTextApp()
   window.show()
   sys.exit(app.exec())

Output

The above code produces the following output −

qgraphicsblur effect two
Advertisements