pyqt5设置treewidget的抬头居中
时间: 2023-07-03 08:28:15 浏览: 183
你可以通过设置treewidget的样式表来将抬头居中。具体操作如下:
```python
# 导入需要的模块
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QTreeWidget, QTreeWidgetItem
# 创建treeWidget
treeWidget = QTreeWidget()
# 设置treewidget的样式表
treeWidget.setStyleSheet("QHeaderView::section {""background-color: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #616161, stop: 0.5 #424242, stop: 0.6 #303030, stop:1 #303030);""color: white;""padding-left: 4px;""border: 1px solid #6c6c6c;""font-weight: bold;""font-size: 12px;""height: 20px;""text-align: center;""}")
# 设置treewidget的抬头居中
treeWidget.header().setDefaultAlignment(Qt.AlignCenter)
# 添加treewidget的抬头
treeWidget.setHeaderLabels(["Name", "Type", "Size"])
# 添加treewidget的item
root = QTreeWidgetItem(treeWidget)
root.setText(0, "root")
root.setText(1, "folder")
root.setText(2, "")
# 显示treewidget
treeWidget.show()
```
运行代码,你会发现treewidget的抬头已经居中了。
阅读全文