from PyQt5 import QtGui, QtWidgets, QtCore
class Test(QtWidgets.QTableWidgetItem):
def __init__(self):
super().__init__()
print("$",self.background()) # note
try_1 = QtGui.QBrush(QtGui.QColor("orange"))
print(try_1)
self.setBackground(try_1)
print("$",self.background()) # remains same
try_2 = QtGui.QBrush(QtGui.QColor("orange"))
print(try_2)
self.setData(QtCore.Qt.BackgroundRole, try_2)
print("$",self.background()) # same
Test()setData()和self.setBackground()不更改QTableWidgetItem的QBrush对象。
,我们可以看到那个自我。背景()返回相同的对象
setForeground和setFont工作,但它不工作的背景有另一种方式来设置背景为QTableWidgetItem,以便,我可以使用它在QHeaderView
我访问过许多类似的帖子,但他们没有任何解决方案。
发布于 2022-09-09 06:01:54
尝试使用以下代码:
self.table.horizontalHeaderItem(col).setBackground(QtGui.QColor("green"))"col“指的是表中的列。注意:在你的应用程序中设置“融合”风格。
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
app.setStyle(QtWidgets.QStyleFactory.create('Fusion')) # won't work on windows style.https://stackoverflow.com/questions/66745604
复制相似问题