qtableweiget添加样式
时间: 2023-12-22 07:04:56 浏览: 24
您可以使用Qt的样式表来为QTableWidget添加样式。样式表是一种基于CSS的语言,可以用来定义Qt应用程序中的控件的外观和布局。
以下是一个简单的例子,演示如何使用样式表为QTableWidget添加样式:
```python
# 引入Qt相关的模块
from PyQt5.QtWidgets import QApplication, QTableWidget, QTableWidgetItem
from PyQt5.QtGui import QColor
from PyQt5.QtCore import Qt
# 创建QApplication实例
app = QApplication([])
# 创建QTableWidget实例
table = QTableWidget()
# 设置表格行列数
table.setRowCount(4)
table.setColumnCount(3)
# 添加表格内容
for row in range(4):
for col in range(3):
item = QTableWidgetItem("Item %d,%d" % (row, col))
table.setItem(row, col, item)
# 设置表格样式
table.setStyleSheet("""
QTableWidget {
background-color: white;
alternate-background-color: #F8F8F8;
color: black;
}
QTableWidget::item:selected {
background-color: #0078D7;
color: white;
}
QHeaderView::section {
background-color: #0078D7;
color: white;
}
""")
# 显示表格
table.show()
# 运行应用程序
app.exec_()
```
以上代码中,我们使用了样式表来设置了QTableWidget的背景颜色、交替行的背景色、文本颜色,以及选中行和表头的颜色。
您可以根据自己的需要来修改样式表,以达到您想要的样式效果。