How to paint on top of QListWidget in eventfilter in PyQt4/PySide?
<p>I tried this on QLable and it works, I do not want to subclass the widget, because they are defined in .ui files, and I just need to do simple modification so I want to avoid delegate. If I put my code in paintEvent it works, but why I can not put it in eventfilter? </p>

<p>It seems it painted the square but it's below the listWidgetItem area.</p>

<pre><code># -*- coding: utf-8 -*-
from PySide.QtCore import *
from PySide.QtGui import *

class xxxx(QListWidget):
    def __init__(self, parent=None):
        super(xxxx, self).__init__(parent)
        self.installEventFilter(self)

    # def paintEvent(self, event):
    #     p = QPainter()
    #     p.begin(self.viewport())
    #     p.setPen(QPen(Qt.black))
    #     p.fillRect(QRect(0, 1, 20, 20), Qt.red)
    #     p.end()

    def eventFilter(self, widget, event):
        if event.type() == QEvent.Paint:
            p = QPainter()
            # p.begin(self.viewport())
            p.begin(widget)
            p.setPen(QPen(Qt.red))
            p.fillRect(QRect(0, 1, 20, 20), Qt.red)
            p.end()
            return True
        return QListWidget.eventFilter(self, widget, event)


if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    view = xxxx()
    view.show()
    sys.exit(app.exec_())
</code></pre>

<p>You need to install the event-filter on the viewport:</p>

<pre><code>class xxxx(QListWidget):
    def __init__(self, parent=None):
        super(xxxx, self).__init__(parent)
        self.viewport().installEventFilter(self)

    def eventFilter(self, widget, event):
        if event.type() == QEvent.Paint:
            p = QPainter()
            p.begin(widget)
            p.setPen(QPen(Qt.red))
            p.fillRect(QRect(0, 1, 20, 20), Qt.red)
            p.end()
            return True
        return super(xxxx, self).eventFilter(widget, event)
</code></pre>

python pyqt pyside qpainter qlistwidget