如何使用 QItemDelegate 更改 QTableView 文本颜色?
How to change QTableView text color using QItemDelegate?
我创建了子类 QItemDelegate 并重新定义了 paint 方法。我试过了
painter.setPen(QtGui.QColor('#FFFFFF'))
这是行不通的。如何更改文字颜色?
作为旁注,我尝试通过调用 painter.setBackground(color) 来为背景着色,但这也不起作用。这些方法打算如何使用?
class ItemDelegate(QtWidgets.QItemDelegate):
def __init__(self, parent):
QtWidgets.QItemDelegate.__init__(self, parent)
self.parent = parent
def paint(self, painter, option, index):
item = index.data(QtCore.Qt.DisplayRole)
#print(item)
print(dir(painter))
if index.column() == 1:
color = QtGui.QColor('#34ebc6')
elif index.column() == 2:
color = QtGui.QColor('#FFFFFF')
elif index.column() == 3:
color = QtGui.QColor('#9546c7')
else:
color = QtGui.QColor('#FFFFFF')
painter.fillRect(option.rect, color)
super(ItemDelegate, self).paint(painter, option, index)
您必须覆盖 drawDisplay 方法,您必须在其中更改调色板的 QPalette::Text 和 QPalette::HighlightedText 角色的值:
class ItemDelegate(QtWidgets.QItemDelegate):
def paint(self, painter, option, index):
color = QtGui.QColor("#FFFFFF")
if index.column() == 1:
color = QtGui.QColor("#34ebc6")
elif index.column() == 2:
color = QtGui.QColor("#FFFFFF")
elif index.column() == 3:
color = QtGui.QColor("#9546c7")
painter._color = color
super(ItemDelegate, self).paint(painter, option, index)
def drawDisplay(self, painter, option, rect, text):
color = painter._color
opt = QtWidgets.QStyleOptionViewItem(option)
cg = (
QtGui.QPalette.Normal
if opt.state & QtWidgets.QStyle.State_Enabled
else QtGui.QPalette.Disabled
)
if opt.state & QtWidgets.QStyle.State_Selected:
opt.palette.setColor(cg, QtGui.QPalette.HighlightedText, color)
opt.palette.setColor(cg, QtGui.QPalette.Text, color)
super(ItemDelegate, self).drawDisplay(painter, opt, rect, text)
使用 QStyledItemDelegate 更容易,因为您只需要覆盖 initStyleOption 方法:
class ItemDelegate(QtWidgets.QStyledItemDelegate):
def initStyleOption(self, option, index):
super(ItemDelegate, self).initStyleOption(option, index)
color = QtGui.QColor("#FFFFFF")
if index.column() == 1:
color = QtGui.QColor("#34ebc6")
elif index.column() == 2:
color = QtGui.QColor("#FFFFFF")
elif index.column() == 3:
color = QtGui.QColor("#9546c7")
cg = (
QtGui.QPalette.Normal
if option.state & QtWidgets.QStyle.State_Enabled
else QtGui.QPalette.Disabled
)
if option.state & QtWidgets.QStyle.State_Selected:
option.palette.setColor(cg, QtGui.QPalette.HighlightedText, color)
option.palette.setBrush(QtGui.QPalette.Text, color)
我创建了子类 QItemDelegate 并重新定义了 paint 方法。我试过了
painter.setPen(QtGui.QColor('#FFFFFF'))
这是行不通的。如何更改文字颜色?
作为旁注,我尝试通过调用 painter.setBackground(color) 来为背景着色,但这也不起作用。这些方法打算如何使用?
class ItemDelegate(QtWidgets.QItemDelegate):
def __init__(self, parent):
QtWidgets.QItemDelegate.__init__(self, parent)
self.parent = parent
def paint(self, painter, option, index):
item = index.data(QtCore.Qt.DisplayRole)
#print(item)
print(dir(painter))
if index.column() == 1:
color = QtGui.QColor('#34ebc6')
elif index.column() == 2:
color = QtGui.QColor('#FFFFFF')
elif index.column() == 3:
color = QtGui.QColor('#9546c7')
else:
color = QtGui.QColor('#FFFFFF')
painter.fillRect(option.rect, color)
super(ItemDelegate, self).paint(painter, option, index)
您必须覆盖 drawDisplay 方法,您必须在其中更改调色板的 QPalette::Text 和 QPalette::HighlightedText 角色的值:
class ItemDelegate(QtWidgets.QItemDelegate):
def paint(self, painter, option, index):
color = QtGui.QColor("#FFFFFF")
if index.column() == 1:
color = QtGui.QColor("#34ebc6")
elif index.column() == 2:
color = QtGui.QColor("#FFFFFF")
elif index.column() == 3:
color = QtGui.QColor("#9546c7")
painter._color = color
super(ItemDelegate, self).paint(painter, option, index)
def drawDisplay(self, painter, option, rect, text):
color = painter._color
opt = QtWidgets.QStyleOptionViewItem(option)
cg = (
QtGui.QPalette.Normal
if opt.state & QtWidgets.QStyle.State_Enabled
else QtGui.QPalette.Disabled
)
if opt.state & QtWidgets.QStyle.State_Selected:
opt.palette.setColor(cg, QtGui.QPalette.HighlightedText, color)
opt.palette.setColor(cg, QtGui.QPalette.Text, color)
super(ItemDelegate, self).drawDisplay(painter, opt, rect, text)
使用 QStyledItemDelegate 更容易,因为您只需要覆盖 initStyleOption 方法:
class ItemDelegate(QtWidgets.QStyledItemDelegate):
def initStyleOption(self, option, index):
super(ItemDelegate, self).initStyleOption(option, index)
color = QtGui.QColor("#FFFFFF")
if index.column() == 1:
color = QtGui.QColor("#34ebc6")
elif index.column() == 2:
color = QtGui.QColor("#FFFFFF")
elif index.column() == 3:
color = QtGui.QColor("#9546c7")
cg = (
QtGui.QPalette.Normal
if option.state & QtWidgets.QStyle.State_Enabled
else QtGui.QPalette.Disabled
)
if option.state & QtWidgets.QStyle.State_Selected:
option.palette.setColor(cg, QtGui.QPalette.HighlightedText, color)
option.palette.setBrush(QtGui.QPalette.Text, color)