'How do I set QModelIndex to emit QAbstractTableModel.dataChanged in PyQT?
I want to refresh content of QTableView when model data changed.
But I can't find a way of specificing the value of QModelIndex instance.
See questions in below code.
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)
Solution 1:[1]
The index method of model is for that.
row_index = model.index(1, 0)
col_index = model.index(1, 0)
model.dataChanged.emit(row_index, col_index, Qt.DisplayRole)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Hill |
