how to update only one row of model in qtableview @ pyside6
-
I want to update only 1 row or 1 cell.
but all cell is updated when datachanged is emitted.
data() was called hundreds times for each cells and each roles.
what is wrong with this code ?
class TestitemTableModel(QAbstractTableModel):
def __init__(self, data): super(TestitemTableModel, self).__init__() self._data = data def data(self, index, role): if role == QtCore.Qt.DisplayRole: value = self._data.iloc[index.row(), index.column()] return str(value) if role == QtCore.Qt.TextAlignmentRole: return int(QtCore.Qt.AlignCenter | QtCore.Qt.AlignVCenter) def setData(self, index, value, role): if not index.isValid(): return False if role == QtCore.Qt.DisplayRole: self._data.iloc[index.row(), index.column()] = 'WTF' self.dataChanged.emit(index, index) return True return QAbstractTableModel.setData(self,index,value,role) def rowCount(self, index): return self._data.shape[0] def columnCount(self, index): return self._data.shape[1] def headerData(self, section, orientation, role): if role == QtCore.Qt.DisplayRole: if orientation == QtCore.Qt.Horizontal: return str(self._data.columns[section]) if orientation == QtCore.Qt.Vertical: return str(self._data.index[section]) -
I want to update only 1 row or 1 cell.
but all cell is updated when datachanged is emitted.
data() was called hundreds times for each cells and each roles.
what is wrong with this code ?
class TestitemTableModel(QAbstractTableModel):
def __init__(self, data): super(TestitemTableModel, self).__init__() self._data = data def data(self, index, role): if role == QtCore.Qt.DisplayRole: value = self._data.iloc[index.row(), index.column()] return str(value) if role == QtCore.Qt.TextAlignmentRole: return int(QtCore.Qt.AlignCenter | QtCore.Qt.AlignVCenter) def setData(self, index, value, role): if not index.isValid(): return False if role == QtCore.Qt.DisplayRole: self._data.iloc[index.row(), index.column()] = 'WTF' self.dataChanged.emit(index, index) return True return QAbstractTableModel.setData(self,index,value,role) def rowCount(self, index): return self._data.shape[0] def columnCount(self, index): return self._data.shape[1] def headerData(self, section, orientation, role): if role == QtCore.Qt.DisplayRole: if orientation == QtCore.Qt.Horizontal: return str(self._data.columns[section]) if orientation == QtCore.Qt.Vertical: return str(self._data.index[section])@Aiden-k
I don't know exactly what your problem is, or what you are trying to do.Qt infrastructure will call
data()multiple times with different roles.Your
setData()sets the data on theDisplayRole. This is wrong.setData()is called withEditRoleto make an update on the value. Start by getting that right.