PyQt - Effect



The effect in PyQt develops a desktop application with a graphical user interface(GUI). This effect can be illustrated through scenarios like data visualization tools, interactive applications, and customized software based on requirements.

The PyQt effect creates a user-friendly application across various domains such as finance software, employment tools, trading platforms, etc.

We have the following standard effect in PyQt −

  • QGraphicsBlurEffect − The items blur by a given radius.
  • QGraphicsDropShadowEffect − This adds a dropshadow effect behind the item.
  • QGraphicsColorizeEffect − This item renders the shades for any given color.
  • QGraphicsOpacityEffect − The item is rendered with an opacity.

In contrast to the GUI effect, a developer can use the standard effect with QGraphicsItem. So, we can create a subclass of the QGraphics Effect. This effect can be installed using the method setGraphicsEffect().

Custom Effect in PyQt

The custom effect can be created using QGraphicsEffect, below is the list of steps that help you to understand this.

Step 1 − Create a new class that inherits from the class QGraphicsEffect. This class is considered a custom effect.

Step 2 − Implement the virtual function using draw() method. The draw() accepts the painter as a parameter and allows the developer to modify the custom visual effect.

Step 3 − To achieve the desired effect, we can call the method sourcePixmap() to obtain a pixmap for the graphics effect. This can be executed within the draw() method.

Step 4 − Use the update method to request a redraw.(This step is required when the effect changes dynamically)

Example

Following example illustrate the custom effect in PyQt using various classes and methods.

from PyQt5.QtWidgets import QApplication, QGraphicsView, QGraphicsScene, 
QGraphicsPixmapItem, QGraphicsEffect, QWidget
from PyQt5.QtGui import QPixmap, QPainter, QColor
from PyQt5.QtCore import Qt

class CustomGraphicsEffect(QGraphicsEffect):
   def __init__(self):
      super().__init__()

   def draw(self, painter: QPainter):
      # Obtain the source pixmap
      source_pixmap, _ = self.sourcePixmap(Qt.DeviceCoordinates)

      # Apply custom modifications to the pixmap (add RGB color)
      modified_pixmap = QPixmap(source_pixmap)
      painter.begin(modified_pixmap)
      painter.setCompositionMode(QPainter.CompositionMode_SourceIn)
      painter.fillRect(modified_pixmap.rect(), QColor(0, 255, 255, 100))
      painter.end()

      # Set the modified pixmap as the output
      self.drawSource(painter)

if __name__ == "__main__":
   app = QApplication([])

   # Create a QWidget to hold the QGraphicsView
   widget = QWidget()
   widget.setWindowTitle("Custom Graphics Effect Example")
   widget.setGeometry(100, 100, 800, 600)

   # Create a QGraphicsView and scene
   view = QGraphicsView(widget)
   scene = QGraphicsScene()
   view.setScene(scene)

   # Load an image (replace with your image path)
   image_path = "C:/Users/Lenovo/Downloads/tutorialspoint.png"
   pixmap_item = QGraphicsPixmapItem(QPixmap(image_path))
   # Apply the custom effect
   pixmap_item.setGraphicsEffect(CustomGraphicsEffect())  
   scene.addItem(pixmap_item)

   widget.show()
   app.exec_()

Output

The above code produces the following output −

pyqt effect intro

Optimization Techniques in PyQt Effect

Optimization plays a crucial role in GUI frameworks, like PyQt and Tkinter, based on application requirements.

Below are some key strategies for optimizing the performance of QGraphicsEffect.

  • The effects are disabled, and the source is rendered directly without any modifications.
  • The method sourceIsPixmap() can be helpful for the optimization process. It allows the developer to check whether the source is an image or other item.
  • While creating custom effects, we must follow the best possible solution for efficient rendering practices such as CPU load, memory usage, and GPU capabilities.

Troubleshooting and Tips in PyQt Effect

When we are working with the PyQt program on QGraphicsEffect, we might encounter some common issues. So, here we have some tips and techniques for debugging and optimization in PyQt application.

We can use standard debugging techniques like print statements and breakpoints. Thus, this identify the issue to our custom effect code.

Use custom effect to change the source of bounding rectangle. For example- if the source is a QGraphicsRectItem and its parameters have changed.

Advantages of GUI in PyQt

Following points to understand the advantage of GUI in PyQt −

  • Graphical User Interface(GUI) is designed for easier interaction between users and computers.
  • GUI performs the productivity of tasks.
  • Users can easily navigate to GUI features with its uses.
  • GUI gives clarity to the software.
  • GUI frameworks and libraries bring multiple advantages to the software development process.
Advertisements