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?
It seems it painted the square but it's below the listWidgetItem area.
# -*- 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_())
You need to install the event-filter on the viewport:
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 and widget is self.viewport():
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)