您可以为整个QTableWidget设置特定的颜色吗?

2024-04-23 19:37:53 发布

您现在位置:Python中文网/ 问答频道 /正文

好的,我们可以为包含数据的单元格设置特定的背景色:

self.Table.item(0,i).setBackground(QColor(255,128,128)) 

是否可以在启动或操作期间将整个表设置为特定颜色?启动时,它不包含任何数据,因此单元格为空


Tags: 数据self颜色tableitem背景色qcolorsetbackground
1条回答
网友
1楼 · 发布于 2024-04-23 19:37:53

如果希望所有单元格都具有该背景色,则可以使用代理:

class Delegate(QStyledItemDelegate):
    def initStyleOption(self, option, index):
        super().initStyleOption(option, index)
        if not index.data(Qt.BackgroundRole):
            option.backgroundBrush = QBrush(QColor(255, 128, 128))
delegate = Delegate(self.Table)
self.Table.setItemDelegate(delegate)

相关问题 更多 >