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).

Qt Code:
  1. def refreshRow(self, task_id, t, val):
  2. record = self.model.record()
  3. ii = record.indexOf("id")
  4. ids = set() # saves the ids for selected tasks
  5. for index in self.select.selectedRows():
  6. ids.add(self.model.data(index))
  7.  
  8. # writing operation itself
  9. for i in xrange(self.model.rowCount()):
  10. index = self.model.index(i, ii)
  11. if self.model.data(index) == task_id:
  12. j = record.indexOf(t)
  13. index = self.model.index(i, j)
  14. if t == "size":
  15. val = int(val)
  16. self.model.setData(index, val)
  17.  
  18. # restoring each selected lines
  19. for i in xrange(self.model.rowCount()):
  20. index = self.model.index(i, ii)
  21. if self.model.data(index) in ids:
  22. 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 ?


Qt Code:
  1. index = self.model.index(0, ii)
  2. self.select.select(index, QtGui.QItemSelectionModel.SelectCurrent | QtGui.QItemSelectionModel.Rows)
To copy to clipboard, switch view to plain text mode 
works fine

Qt Code:
  1. index = self.model.index(1, ii)
  2. self.select.select(index, QtGui.QItemSelectionModel.SelectCurrent | QtGui.QItemSelectionModel.Rows)
To copy to clipboard, switch view to plain text mode 
selects the correct line

Qt Code:
  1. index = self.model.index(0, ii)
  2. self.select.select(index, QtGui.QItemSelectionModel.SelectCurrent | QtGui.QItemSelectionModel.Rows)
  3. index = self.model.index(1, ii)
  4. 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