QtQTableView设置表头颜色为灰色
要设置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的表头颜色被设置为灰色。
原文地址: https://www.cveoy.top/t/topic/i6eS 著作权归作者所有。请勿转载和采集!