QtableWidget 列保持在左侧显示
时间: 2024-03-26 16:40:40 浏览: 18
要将 QTableWidget 中的列保持在左侧显示,可以使用 setVerticalHeader 方法来设置垂直表头,并将其设置为 Fixed 行模式。这将使垂直表头保持在左侧固定位置,而列内容可以在滚动时自由滚动。
以下是一个示例代码片段,演示如何将 QTableWidget 的列保持在左侧显示:
```
# 创建 QTableWidget 对象
tableWidget = QTableWidget()
# 设置表格的行和列数量
tableWidget.setRowCount(10)
tableWidget.setColumnCount(5)
# 设置垂直表头,并将其设置为 Fixed 行模式
header = tableWidget.verticalHeader()
header.setSectionResizeMode(QHeaderView.Fixed)
# 添加表格内容
for row in range(10):
for col in range(5):
item = QTableWidgetItem("Row %s, Col %s" % (row, col))
tableWidget.setItem(row, col, item)
# 显示表格
tableWidget.show()
```
在上面的示例中,我们首先创建了一个 QTableWidget 对象,并设置了表格的行和列数量。然后,我们获取了表格的垂直表头,并将其设置为 Fixed 行模式。最后,我们添加了一些表格内容,并显示了表格。这样,列就会保持在左侧显示了。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)