How to add a QGraphicsEffect to a QAbstractItemView in Qt?

308 views Asked by At

I'm trying to add a QGraphicsEffect to a QAbstractItemView (could be a QListView, QTableView, it should be similar for all of them) in Qt (using PySide2 in Python 2.7, but should apply to any language with Qt).

I have a suspicion that due to the way the item view widgets render themselves on a per-item basis with render delegates etc etc, that they won't play nicely and there isn't a solution. I can't find a reference to it explicitly NOT working in the docs, so it seems like it should work (I mean, they are subclasses of QWidget, which does support QGraphicsEffects. Seems like a possible oversight in implementation/documentation). Can anyone confirm or help with the correct way to do or workaround this?

Example that demonstrates with a blur effect:

from PySide2 import QtWidgets, QtGui, QtCore

lw = QtWidgets.QListWidget()
lw.addItems(["dog", "cat", "fish", "platypus"])
lw.show()

ge = QtWidgets.QGraphicsBlurEffect()
lw.setGraphicsEffect(ge)
ge.setBlurRadius(20)

btn = QtWidgets.QPushButton("so fuzzy!")
btn.show()

ge = QtWidgets.QGraphicsBlurEffect()
btn.setGraphicsEffect(ge)
ge.setBlurRadius(20)

Screenshot:

enter image description here

1

There are 1 answers

2
eyllanesc On BEST ANSWER

You have to apply effect to the viewport:

from PySide2 import QtWidgets

app = QtWidgets.QApplication()

lw = QtWidgets.QListWidget()
lw.addItems(["dog", "cat", "fish", "platypus"])
lw.show()

ge = QtWidgets.QGraphicsBlurEffect()
lw.viewport().setGraphicsEffect(ge)
ge.setBlurRadius(20)

btn = QtWidgets.QPushButton("so fuzzy!")
btn.show()

ge2 = QtWidgets.QGraphicsBlurEffect()
btn.setGraphicsEffect(ge2)
ge2.setBlurRadius(20)

app.exec_()