PDA

View Full Version : Refreshing QTableView data without resetting whole view



Tiansen
1st February 2014, 21:19
I have another problem, that I try to solve for a while now, but without (real) success.

Let's assume we have QTableView that is populated by QSqlQueryModel data. Now when I change some data inside model, I want to refresh view too. If I do this with running setQuery again, then view does refresh itself, but(!) the whole view also resets itself. This is really annoying if user looks at a long table, and for example has one row selected. When view refreshes, his selection is gone and he must go looking for that particular row again. So I would need to refresh view without resetting the whole thing. How do I do that?

d_stranz
2nd February 2014, 02:14
Think about the general case of your problem: you have a table view connected to an SQL query model, you issue new queries on the database, and you repopulate the view with the results. In the general case, the new quesry results will have nothing in common with the previous results, so re-selecting the same row afterwards makes no sense at all, because the contents of the row will probably be completely different.

On the other hand, if the query always returns the same rows, but the contents of the fields within the row might be updated, then you have a more restricted case and it might make sense to keep track of the current row and re-select it after the update.

Are you using a QItemSelectionModel on the view? You can use it to keep track of the current selection, and then after the refresh, use it to select the same row again.

Also remember that QModelIndex instances are transient. If the model refreshes because you've run a new query, then none of the indexes from the previous model will be valid. You'll need to keep track of the actual row number, and use it to construct a QModelIndex (using QAbstractItemModel::createIndex()) that you can pass into the QItemSelectionModel::select() slot.

Question for the admins: why is it that all of the QTCLASS references go to the docs for Qt 4.8 instead of the current Qt5 release?

Tiansen
2nd February 2014, 11:22
Very good, d_stranz. I solved it as you suggested. Thank you very much!

Do you maybe have an idea how to solve my another issue?
Link: http://www.qtcentre.org/threads/57898-Trouble-with-sql-views-and-sorting