What's different between QListView and QTableView in paintEvent?

550 views Asked by At

I have tested customized QGraphicsEffect and founded a weird case.
I applied the EFFECT on my dialog and it does not work in QListView.

Here is simple test,

from PySide2.QtCore import Qt, QPoint
from PySide2.QtWidgets import QDialog, QGraphicsEffect, QVBoxLayout, QHBoxLayout, QPushButton, QApplication, QListView, QTreeView, QTableView
from PySide2.QtGui import QTransform
import sys

class DarkenEffect(QGraphicsEffect):
    def draw(self, painter):
        offset = QPoint()
        if self.sourceIsPixmap():
            pixmap = self.sourcePixmap(Qt.LogicalCoordinates, offset)
        else:
            pixmap = self.sourcePixmap(Qt.DeviceCoordinates, offset)
            painter.setWorldTransform(QTransform())

        painter.setBrush(Qt.black)
        painter.drawRect(pixmap.rect())
        painter.setOpacity(0.5)
        painter.drawPixmap(offset, pixmap)  

class Dialog(QDialog):
    def __init__(self, parent=None):
        QDialog.__init__(self, parent)
        self.setupUi()

        effect = DarkenEffect(self)
        self.setGraphicsEffect(effect)

    def setupUi(self):
        layout = QVBoxLayout()
        self.setLayout(layout)
        listview = QListView()
        layout.addWidget(listview)
        treeview = QTreeView()
        layout.addWidget(treeview)
        tableview = QTableView()
        layout.addWidget(tableview)
        button1 = QPushButton("BUTTON1")
        layout.addWidget(button1)
        button2 = QPushButton("BUTTON2")
        layout.addWidget(button2)
        hLayout = QHBoxLayout()
        button3 = QPushButton("BUTTON3")
        hLayout.addWidget(button3)
        button4 = QPushButton("BUTTON4")
        hLayout.addWidget(button4)
        layout.addLayout(hLayout)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    dlg = Dialog()
    dlg.show()
    sys.exit(app.exec_())

after running the test code, when mouse cursor above the QListView or QTreeView, the effect disappears. But above the QTableView, it keeps on.
I just wonder if there is any difference between QListView and QTableView.

0

There are 0 answers