I have a QTableView, filled with the data coming from a QSqlTableModel.Each line of the QTableView represents an "article", coming from a website. The articles can be read or unread, exactly like an email. The state read/unread is stored in the database, under the field "new".
If the article is unread, it is displayed in a certain way in the view. If it is read, it is displayed on another way, like an email in any email manager.
When the user clicks on a article unread, the article is marked read, which means the model storing the data is updated:
def markOneRead(self, element):
"""Slot to mark an article read"""
table = self.liste_tables_in_tabs[self.onglets.currentIndex()]
model = self.liste_models_in_tabs[self.onglets.currentIndex()]
proxy = self.liste_proxies_in_tabs[self.onglets.currentIndex()]
new = table.model().index(element.row(), 12).data()
if new == 0:
return
else:
# Save the current line
line = table.selectionModel().currentIndex().row()
# Update the model
table.model().setData(table.model().index(line, 12), 0)
try:
# Try to update the model
model.setQuery(self.query)
proxy.setSourceModel(model)
table.setModel(proxy)
except AttributeError:
pass
# Once the model is updated, reselect the line saved
table.selectRow(line)
While this method works most of the time, I only save the current line number in the variable "line". So if the model has changed (from any other process of the program), when I do table.selectRow(line), I select the same line yes, but it can be a completely different item.
So I need a more precise way to store the clicked item, before I update the model.
Do you have any idea ?