How to set the QGraphicsDropShadowEffect on the drop down menu of a QComboBox?
-
I tried it like this
self.combobox.view().setGraphicsEffect(self.shadow_effect)
but it doesn't work. -
-
Instead of applying the shadow to the view, try setting it on the entire ComboBox or its delegate items.
Apply Shadow to the Entire ComboBox:
from PyQt5.QtWidgets import QApplication, QComboBox, QGraphicsDropShadowEffect
from PyQt5.QtGui import QColor
from PyQt5.QtCore import Qtapp = QApplication([])
combobox = QComboBox()
combobox.addItems(["Option 1", "Option 2", "Option 3"])Create shadow effect
shadow_effect = QGraphicsDropShadowEffect()
shadow_effect.setBlurRadius(10)
shadow_effect.setOffset(3, 3)
shadow_effect.setColor(QColor(0, 0, 0, 100))Apply to ComboBox
combobox.setGraphicsEffect(shadow_effect)combobox.show()
app.exec_()
If you only want the shadow effect on the dropdown list items, you must customize the item delegate.
from PyQt5.QtWidgets import QApplication, QComboBox, QStyledItemDelegate, QGraphicsDropShadowEffect
from PyQt5.QtGui import QColor
from PyQt5.QtCore import Qtclass ShadowDelegate(QStyledItemDelegate):
def paint(self, painter, option, index):
shadow = QGraphicsDropShadowEffect()
shadow.setBlurRadius(10)
shadow.setOffset(3, 3)
shadow.setColor(QColor(0, 0, 0, 100))painter.setPen(Qt.NoPen) shadow.drawSource(painter) super().paint(painter, option, index)
app = QApplication([])
combobox = QComboBox()
combobox.addItems(["Option 1", "Option 2", "Option 3"])
combobox.setItemDelegate(ShadowDelegate())combobox.show()
app.exec_() -
@Shankarlinga-M IDK about PyQt5, but in PyQt6, setting the shadow effect on the combobox itself will only apply the shadow on the combobox, but not the dropdown
self.combobox = QComboBox(self) self.apply_static_shadow(self.combobox) def apply_static_shadow(self, widget): shadow = QGraphicsDropShadowEffect() shadow.setOffset(5, 5) shadow.setColor(QColor("blue")) widget.setGraphicsEffect(shadow)
-
In PyQt6, setting a QGraphicsDropShadowEffect on the QComboBox itself only applies the effect to the main widget, not the dropdown menu. To apply the shadow effect to the dropdown, you need to set the effect on the QComboBox's view().
Here's how you can apply a drop shadow effect to the dropdown menu in PyQt6:
from PyQt6.QtWidgets import QApplication, QComboBox, QGraphicsDropShadowEffect
from PyQt6.QtGui import QColor
from PyQt6.QtCore import Qtapp = QApplication([])
Create ComboBox
combobox = QComboBox()
combobox.addItems(["Option 1", "Option 2", "Option 3"])Create shadow effect
shadow_effect = QGraphicsDropShadowEffect()
shadow_effect.setBlurRadius(10)
shadow_effect.setOffset(5, 5)
shadow_effect.setColor(QColor(0, 0, 255, 100)) # Blue shadow with transparencyApply shadow effect to dropdown menu (view)
combobox.view().setGraphicsEffect(shadow_effect)combobox.show()
app.exec() -
@Shankarlinga-M Look at my original post, that's what I tried and it doesn't seem to work, have you tested this? I'm on PyQt6 Qt version 6.8.2
-
@Shankarlinga-M I did some testing and it seems that the style from
QStyleFactory
can override the behavior of effects in the combobox'sview()
andview().viewport()
You can try printing the available styles and changing them like this:
print(QStyleFactory.keys()) app = QApplication(sys.argv) app.setStyle(QStyleFactory.create('Windows'))
It also seems that the
QGraphicsDropShadowEffect()
expands the bounding box of the widget you apply it to, but doesn't expand the bounding box of thecombobox.view()
6/6