paintEvent中的QListView和QTableView有什么不同?

时间:2018-05-08 09:07:11

标签: qt qtableview qlistwidget pyside2 qgraphicseffect

我测试了定制的QGraphicsEffect并创建了一个奇怪的案例 我在我的对话框中应用了 EFFECT ,它在QListView中不起作用。

这是简单的测试,

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_())

运行测试代码后,当鼠标光标位于QListView或QTreeView上方时,效果消失。但在QTableView之上,它继续 我只是想知道QListView和QTableView之间是否有任何区别。

0 个答案:

没有答案