Hello people !
I have been thinking about for one whole day, but I must confess that I'm pretty stuck now...
I have a QTableView, linked to a QSqlTableModel.
The model is set to OnFieldChange, so that the modifications are immediately written to the db.
As a consequence, every setData which is executed refreshes my model, and I lose my current selection.
So what I'm doing is saving the selection state before writing, and restore it afterwards (this is PyQt code, but I think it's easy enough to understand).
def refreshRow(self, task_id, t, val):
record = self.model.record()
ii = record.indexOf("id")
ids = set() # saves the ids for selected tasks
for index in self.select.selectedRows():
ids.add(self.model.data(index))
# writing operation itself
for i in xrange(self.model.rowCount()):
index = self.model.index(i, ii)
if self.model.data(index) == task_id:
j = record.indexOf(t)
index = self.model.index(i, j)
if t == "size":
val = int(val)
self.model.setData(index, val)
# restoring each selected lines
for i in xrange(self.model.rowCount()):
index = self.model.index(i, ii)
if self.model.data(index) in ids:
def refreshRow(self, task_id, t, val):
record = self.model.record()
ii = record.indexOf("id")
ids = set() # saves the ids for selected tasks
for index in self.select.selectedRows():
ids.add(self.model.data(index))
# writing operation itself
for i in xrange(self.model.rowCount()):
index = self.model.index(i, ii)
if self.model.data(index) == task_id:
j = record.indexOf(t)
index = self.model.index(i, j)
if t == "size":
val = int(val)
self.model.setData(index, val)
# restoring each selected lines
for i in xrange(self.model.rowCount()):
index = self.model.index(i, ii)
if self.model.data(index) in ids:
self.select.select(index, QtGui.QItemSelectionModel.SelectCurrent | QtGui.QItemSelectionModel.Rows)
To copy to clipboard, switch view to plain text mode
If one row is selected, it is restored correctly.
In case I have several ones, only one of them is selected back.
My problem seems to be in my last line, but even reading the docs doesn't help : it should work, shouldn't it ?
index = self.model.index(0, ii)
index = self.model.index(0, ii)
self.select.select(index, QtGui.QItemSelectionModel.SelectCurrent | QtGui.QItemSelectionModel.Rows)
To copy to clipboard, switch view to plain text mode
works fine
index = self.model.index(1, ii)
index = self.model.index(1, ii)
self.select.select(index, QtGui.QItemSelectionModel.SelectCurrent | QtGui.QItemSelectionModel.Rows)
To copy to clipboard, switch view to plain text mode
selects the correct line
index = self.model.index(0, ii)
index = self.model.index(1, ii)
index = self.model.index(0, ii)
self.select.select(index, QtGui.QItemSelectionModel.SelectCurrent | QtGui.QItemSelectionModel.Rows)
index = self.model.index(1, ii)
self.select.select(index, QtGui.QItemSelectionModel.SelectCurrent | QtGui.QItemSelectionModel.Rows)
To copy to clipboard, switch view to plain text mode
selects just one line... something is definitely wrong in there ?
Thanks for your help
Bookmarks