qt tableview更改行号
时间: 2023-08-24 15:07:12 浏览: 63
如果你想更改`QTableView`的行号,你可以使用`QHeaderView`来实现。
以下是一个简单的示例代码,演示如何更改行号:
```python
from PyQt5.QtWidgets import QApplication, QTableView, QHeaderView, QMainWindow, QAbstractItemView
from PyQt5.QtGui import QStandardItemModel, QStandardItem
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.table_view = QTableView()
self.setCentralWidget(self.table_view)
# 设置模型
model = QStandardItemModel(4, 2)
for i in range(model.rowCount()):
for j in range(model.columnCount()):
item = QStandardItem("Row {} Column {}".format(i+1, j+1))
model.setItem(i, j, item)
self.table_view.setModel(model)
# 隐藏表头
self.table_view.verticalHeader().setVisible(False)
self.table_view.horizontalHeader().setVisible(False)
# 设置行号
header = self.table_view.verticalHeader()
header.setSectionResizeMode(QHeaderView.Fixed)
header.setDefaultSectionSize(30)
for i in range(model.rowCount()):
header.setSectionHidden(i, False)
item = QStandardItem(str(i+1))
model.setVerticalHeaderItem(i, item)
# 允许多选
self.table_view.setSelectionMode(QAbstractItemView.MultiSelection)
if __name__ == "__main__":
app = QApplication([])
window = MainWindow()
window.show()
app.exec_()
```
在这个例子中,我们隐藏了原始的行号,然后手动设置了新的行号。我们使用了`QHeaderView`来自定义行号,设置了固定的行高和默认的行高,然后在每个行上设置了一个新的行号。你可以通过更改`setDefaultSectionSize`方法的参数来更改行高。
注意,我们还启用了多选模式,这样用户就可以同时选择多行。
相关推荐
![](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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)