在python环境中,可以自定义Qtableview单元格颜色,包括前景色和背景色。
change_color中按角色设定了颜色值;data中按角色修改颜色值。
class CustomTableModel(QAbstractTableModel):
def __init__(self):
super().__init__()
def data(self, index, role=Qt.DisplayRole):
if role == Qt.DisplayRole:
return self.input_data[index.row()][index.column()]
elif role == Qt.ForegroundRole:
if index.row() == 0 and index.column() == 1: # 第一行字体颜色为红色
return QColor(Qt.red)
elif index.row() == 1: # 第二行字体颜色为绿色
return QColor(Qt.green)
color = self.colors.get((index, role))
if color is not None:
return color
elif role == Qt.FontRole:
# QColor(Qt.red)
if index.row() == 2: # 第三行字体大小为15
font = QFont()
font.setPointSize(15)
return font
elif role == Qt.BackgroundRole:
color =

文章介绍了如何在Python环境中使用QTableview自定义单元格的颜色,包括前景色和背景色,通过CustomTableModel类展示如何按角色设置和修改颜色,以及TableWidget中的方法来设置特定单元格的颜色。
最低0.47元/天 解锁文章
952





