1

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

eyllanesc
  • 235,170
  • 19
  • 170
  • 241

1 Answers1

2

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_()
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Aha! that does it. Hadn't thought to look at `QAbstractScrollArea` as a part of the issue. I now see that perhaps I was wrong in how I asked my question though, as I'm seeking to apply an effect to a number of widgets by applying an effect to the parent of them all. This works well for widgets without a viewport, but in the item view when you highlight items they redraw without the effect at, but changes that cause a redraw of all widgets (resize, show, etc) make them draw fuzzy. Is there a way to remedy this? – Mitchell Kehn Aug 23 '20 at 23:06
  • @MitchellKehn If you have another question then you must create another post, those are the SO rules – eyllanesc Aug 23 '20 at 23:10