简体   繁体   English

QTableWidgetItem 的 setBackground 不起作用

[英]setBackground for the QTableWidgetItem doesnt work

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() and self.setBackground() doesn't change the QBrush object of QTableWidgetItem . setData() 和 self.setBackground() 不会改变QTableWidgetItem的 QBrush object 。
we can see that self.我们可以看到那个自己。 background() returns the same object background() 返回相同的 object

setForeground and setFont works, but it doesn't work for the Background is there another way to set background for the QTableWidgetItem so that i can use it in QHeaderView ? setForeground 和 setFont 有效,但它不适用于 Background 是否有另一种方法可以为 QTableWidgetItem 设置背景,以便我可以在 QHeaderView 中使用它

I have visited lots of similar posts but they didn't have any solutions.我访问过很多类似的帖子,但他们没有任何解决方案。

Try to using the following code:尝试使用以下代码:

self.table.horizontalHeaderItem(col).setBackground(QtGui.QColor("green"))

"col" refers to the column of your table. “col”是指表格的列。 Note: set 'Fusion' style in your app.注意:在您的应用中设置“融合”样式。

if __name__ == "__main__":
  app = QtWidgets.QApplication(sys.argv)
  app.setStyle(QtWidgets.QStyleFactory.create('Fusion')) # won't work on windows style.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM