要设置 QTableView 的表头颜色为灰色,可以使用 QHeaderView 的 setStyleSheet 方法来设置样式表。以下是一个示例代码:

from PyQt5 import QtWidgets, QtGui

app = QtWidgets.QApplication([])
table = QtWidgets.QTableView()

# 设置表头样式
header = table.horizontalHeader()
header.setStyleSheet('QHeaderView::section { background-color: gray; }')

# 添加数据
model = QtGui.QStandardItemModel(4, 4)
for row in range(4):
    for column in range(4):
        item = QtGui.QStandardItem(f'({row}, {column})')
        model.setItem(row, column, item)
table.setModel(model)

table.show()
app.exec_()

在上面的代码中,首先通过 table.horizontalHeader() 获取水平表头,然后使用 setStyleSheet 方法设置样式表。在样式表中,QHeaderView::section 表示表头部分,background-color 表示背景颜色,这里设置为灰色。

然后,我们创建了一个 QStandardItemModel 并添加了一些数据,将其设置给 QTableView。最后,我们调用 table.show() 显示窗口,并通过 app.exec_() 进入 Qt 的事件循环中。

运行代码后,将会看到 QTableView 的表头颜色被设置为灰色。

Qt QTableView 表头颜色设置:如何将表头设置为灰色

原文地址: https://www.cveoy.top/t/topic/mtoB 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录