PDA

View Full Version : Maintaining QTableView Selection through submitAll()



ChrisW67
27th July 2009, 00:33
G'day All

I have a QTableView displaying data from a QSqlTableModel derivative. The user can select a row in the view and open a dialog to edit the details. When the user opts to commit the changes in the dialog, submitAll() is called as the dialog is closed. This action clears the table view selection. I'd like to keep the edited row selected but I'm having trouble doing so. (When the user cancels the dialog, revertAll() is used to discard any changes, and the selection remains valid.)

I initially tried the lightly documented QPersistentIndex to find that they are not valid after the submitAll() and consequently not very useful. In http://www.qtcentre.org/forum/f-qt-programming-2/t-qpersistentmodelindex-and-qdatawidgetmapper-14519.html/?highlight=persistent Wysota suggests storing the primary key of the row (which I can do) and re-selecting it after the submitAll(). How do I achieve this? The only way I can see is iterating over the model to find the row with the same primary key, something that strikes me as not very efficient.

I'd like to do something similar with deleting rows, leaving the row above or below selected afterward, and adding rows where the new row is selected afterward. For deleting the stored primary key approach will work. For adding rows, I don't have a primary key to store. How do I tackle it then?

Any thought appreciated,
Chris W