【发布时间】:2021-03-18 10:08:43
【问题描述】:
我创建了子类 QItemDelegate 并重新定义了绘制方法。我试过了
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)
【问题讨论】:
-
文本颜色是从 QPalette::Text 使用的,所以你应该改变使用的调色板。
-
感谢您的回复。我不跟随。我在绘画功能中改变这个吗?像这样的调色板 = QtGui.QPalette() palette.setColor(QtGui.QPalette.Text, QtCore.Qt.blue)painter.setPen(palette)
标签: python qt pyqt5 pyside pyside2