考虑这个示例,我想将“垂直”背景应用于表格第三列中的所有单元格:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import (Qt, QPointF)
from PyQt5.QtGui import (QColor, QGradient, QLinearGradient, QBrush)
# starting point from https://www.pythonguis.com/tutorials/qtableview-modelviews-numpy-pandas/
class TableModel(QtCore.QAbstractTableModel):
def __init__(self, data):
super(TableModel, self).__init__()
self._data = data
self.bg_col1 = QColor("#A3A3FF")
self.bg_col2 = QColor("#FFFFA3")
self.bg_grad = QLinearGradient(QPointF(0.0, 0.0), QPointF(0.0, 1.0)) # setcolor 0 on top, 1 on bottom
self.bg_grad.setCoordinateMode(QGradient.ObjectMode) #StretchToDeviceMode) #ObjectBoundingMode)
self.bg_grad.setSpread(QGradient.PadSpread) #RepeatSpread) # PadSpread (default)
self.bg_grad.setColorAt(0.0, self.bg_col1)
self.bg_grad.setColorAt(1.0, self.bg_col2)
self.bg_grad_brush = QBrush(self.bg_grad)
#
def data(self, index, role):
if role == Qt.DisplayRole:
# See below for the nested-list data structure.
# .row() indexes into the outer list,
# .column() indexes into the sub-list
return self._data[index.row()][index.column()]
if role == Qt.BackgroundRole:
if index.column() == 2:
return self.bg_grad_brush
#
def rowCount(self, index):
# The length of the outer list.
return len(self._data)
#
def columnCount(self, index):
# The following takes the first sub-list, and returns
# the length (only works if all rows are an equal length)
return len(self._data[0])
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.table = QtWidgets.QTableView()
data = [
[4, 9, 2],
[1, 0, 0],
[3, 5, 0],
[3, 3, 2],
[7, 8, 9],
]
self.model = TableModel(data)
self.table.setModel(self.model)
self.setCentralWidget(self.table)
app=QtWidgets.QApplication(sys.argv)
window=MainWindow()
window.show()
app.exec_()
当我运行此代码时,首先我得到这张图:
只有第一行的单元格具有我想象的渐变;但它下面的所有其他内容都显示为单色。
我可以注意到,如果我调整第二行高度的大小,那里的相关单元格中有一个渐变,但它在某种程度上是错误的 - 它没有按比例拉伸到单元格的边界:
如果我随后调整第 1 行的高度,那么我可以看到该行中具有背景的单元格根据我的预期“拉伸”背景渐变 - 与此同时,它下面的单元格丢失了之前显示的渐变:
我能做什么,以便所有具有渐变BackgroundRole的单元格的行为与第一个行中的单元格相同(显示完整渐变并根据单元格大小拉伸),并且如果另一个单元格更改大小,则不会“丢失”其渐变渲染?
问题是由于大多数 QStyle 使用默认的 QCommonStyle 实现来绘制项目的背景,该实现在使用给定的画笔填充之前根据索引矩形调用
QPainter.setBrushOrigin()
。
由于渐变处于“对象模式”,渐变仍然使用给定对象的边界矩形(项目矩形)作为其颜色的参考,但由于样式更改了画笔原点,所以结果是渐变为转移了。
如果您使用视图的
ScrollPerPixel
垂直滚动模式,或者使用 RepeatSpread
,这可能会更清楚,它显示渐变的“开始”根据项目的左上角矩形而变化,并且只有当项目的垂直原点恰好是其高度的倍数时才变得“正确”。
因此,一种可能的解决方法是对画笔进行负垂直平移,这样当样式尝试调整画家时,它实际上会显示在应该显示的位置,因为渐变“移回到”其实际原点,相对于该项目是正确的。
这可以通过使用委托轻松实现:
class GradientDelegate(QStyledItemDelegate):
def initStyleOption(self, opt, index):
super().initStyleOption(opt, index)
if (
isinstance(opt.backgroundBrush, QBrush)
and opt.backgroundBrush.gradient()
):
opt.backgroundBrush.setTransform(
QTransform().translate(-opt.rect.x(), -opt.rect.y()))
class MainWindow(QMainWindow):
def __init__(self):
...
self.table.setItemDelegateForColumn(2, GradientDelegate(self.table))