PyQt - QGraphicsDropShadowEffect



Suppose we have a button or image on our computer screen and we want to make it more interactive.

Then we have a class like QGraphicsDropShadowEffect which helps to add a shadow behind the button or picture and make it stand out. We can modify the shadow color, blurred-effect, or make it any specific look.

The appearance of drop shadow effect uses the class QGraphicsDropShadowEffect. This class has same name of function which can used to render the source with a drop shadow.

We have three different uses of function that perform the task of drop shadow. Below is the list of following points −

  • A color of drop shadow can be modified using the setColor() function.
  • The drop shadow offset can be modified using the setOffset() function.
  • We can apply blur radius to drop shadow which can be possible through the function setBlurRadius().

Note that the default color of drop shadow is a semi-transparent dark grey i.e. QColor(63, 63, 63, 180) and it is specified within the device coordinates.

Example 1

In this example, we illustrate the program of drop shadow button using various classes and methods in PyQt.

from PyQt6.QtWidgets import QApplication, QWidget, QPushButton, 
QVBoxLayout, QGraphicsDropShadowEffect
from PyQt6.QtGui import QColor
def create_drop_shadow():
   # Create a button
   button = QPushButton("Click Here")

   # Create a QGraphicsDropShadowEffect
   shadow_effect = QGraphicsDropShadowEffect()

   # Set the shadow color
   shadow_effect.setColor(QColor(85, 85, 93, 180))

   # Set the shadow offset (x, y)
   shadow_effect.setOffset(8, 8)

   # Set the blur radius
   shadow_effect.setBlurRadius(1)

   # Apply the effect to the button
   button.setGraphicsEffect(shadow_effect)
   return button

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

   # Create a main window
   main_window = QWidget()
   main_window.setGeometry(100, 100, 300, 200)
   main_window.setWindowTitle("QGraphicsDropShadowEffect")

   # Create a layout
   layout = QVBoxLayout(main_window)

   # Create a button with a drop shadow
   shadow_button = create_drop_shadow()

   # Add the button to the layout
   layout.addWidget(shadow_button)

   # Show the main window
   main_window.show()

   # Start the application event loop
   app.exec()

Output

The above code produces the following output −

qgraphicsdropshadoweffect ex one

Example 2

In this example, we display the graphical shape of a rectangle using the method QGraphicsRectItem() and the formation of shadow occurs using various functions of PyQt such as setColor(), offset(), setBlurRadius(), and setGraphicsEffect().

import sys
from PyQt6.QtWidgets import QApplication, QGraphicsView, QGraphicsScene, 
QGraphicsRectItem, QGraphicsDropShadowEffect
from PyQt6.QtGui import QColor, QPainter, QBrush
def create_shadow_rect():
   # Create a rectangle and adjust the position by specifying coordinates
   rect = QGraphicsRectItem(70, 70, 300, 200)  
   # Create a QGraphicsDropShadowEffect
   shadow_effect = QGraphicsDropShadowEffect()
   # Set the color of shadow
   shadow_effect.setColor(QColor(89, 89, 78, 200))
   # Set the offset(x, y) for shadow
   shadow_effect.setOffset(8, 8)
   # Set the blur radius
   shadow_effect.setBlurRadius(1)  
   # Apply the effect to the rectangle
   rect.setGraphicsEffect(shadow_effect)
   return rect

if __name__ == "__main__":
   app = QApplication(sys.argv)

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

   # Create a shadowed rectangle
   shadow_rect = create_shadow_rect()
   scene.addItem(shadow_rect)

   # Show the view
   view.show()
   sys.exit(app.exec())

Output

The above code produces the following output −

qgraphicsdropshadoweffect ex two
Advertisements