QTable view如何获取所选列的表头或者列号
时间: 2024-02-27 19:51:32 浏览: 20
您可以使用QTableView的selectedIndexes()方法获取所选单元格的QModelIndex对象列表,然后通过QModelIndex的column()方法获取所选单元格的列号。而要获取列头,您可以使用QTableView的horizontalHeader()方法获取QHeaderView对象,然后通过QHeaderView的visualIndex()方法将列号转换为可视化的列号,最后使用QHeaderView的model()->headerData()方法获取列头数据。示例代码如下:
``` python
# 获取所选单元格的列号
selected_indexes = table_view.selectedIndexes()
selected_columns = set(index.column() for index in selected_indexes)
# 获取所选列的表头
header_view = table_view.horizontalHeader()
for column in selected_columns:
visual_column = header_view.visualIndex(column)
header_data = header_view.model().headerData(visual_column, Qt.Horizontal)
print(f"Column {column} header: {header_data}")
```
其中,table_view是您的QTableView对象,Qt是您所使用的Qt库。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)