当模型数据发生变化时,我想刷新QTableView
的内容。
但是我找不到一种方法来说明QModelIndex
实例的值。
参见下面代码中的问题。
from PyQt5 import QtWidgets, QtCore
from PyQt5.QtCore import QModelIndex, Qt
class MyTableModel(QtCore.QAbstractTableModel):
def __init__(self, data=[[]], parent=None):
super().__init__(parent)
self.data = data
...
data = [
[1, 2],
[3, 4],
]
model = MyTableModel(data)
view = QtWidgets.QTableView()
view.setModel(model)
# changing a data element at row 1, column 0
data[1][0] = 30
row_index = QModelIndex()
# Question: how do I set row_index as 1?
col_index = QModelIndex()
# Question: how do I set col_index as 0?
model.dataChanged.emit(row_index, col_index, Qt.DisplayRole)
发布于 2022-02-26 14:10:26
模型的index
方法就是这样做的。
row_index = model.index(1, 0)
col_index = model.index(1, 0)
model.dataChanged.emit(row_index, col_index, Qt.DisplayRole)
https://stackoverflow.com/questions/71277230
复制相似问题