我测试了定制的 QGraphicsEffect 并建立了一个奇怪的案例。
我在对话框上应用了效果,但它在 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 之间是否有任何区别。