qsqltablemodel,获取修改的行
时间: 2023-06-25 12:03:46 浏览: 39
如果您想在 QTableView 中编辑数据并获取修改的行,可以使用 QTableView 的 `model()` 方法获取其模型,然后使用 `dataChanged()` 信号来检测数据的更改。
以下是一个示例代码:
```python
from PyQt5.QtCore import QModelIndex
table_view = QTableView()
table_model = QSqlTableModel()
table_view.setModel(table_model)
def on_data_changed(top_left: QModelIndex, bottom_right: QModelIndex):
for row in range(top_left.row(), bottom_right.row() + 1):
# 获取修改的行
modified_row = []
for col in range(table_model.columnCount()):
index = table_model.index(row, col)
modified_row.append(table_model.data(index))
print(modified_row)
table_model.dataChanged.connect(on_data_changed)
```
在上面的代码中,我们连接了 `dataChanged()` 信号到一个名为 `on_data_changed()` 的方法。当数据在表格中更改时,该方法将被调用。在该方法中,我们可以遍历更改的行并获取修改的数据。