PDA

View Full Version : QPersistentModelIndex usage info request



defumar
17th January 2008, 18:48
I've searched for some info about the usage of the QPersistentModelIndex, sadly I found nothing about this. Could anyone enlighten me on the topic of using persistent model indices as I don't know what to do to get them to work with my own database model (seriously one can't expect the QPersistentModelIndex to magically know that by storing the unique index of the database table it will be as persistent as it can be).

First I searched the qt reference pages and all I could find was the page stating what the persistent index can do but not how it could be implemented with a different model or so. I checked this forum and googled quite a bit but with no results. Either there's nothing to be found or my searching skills aren't many.

Thanks in advance.

wysota
17th January 2008, 19:58
A model index has nothing to do with database indexes. It's a "pointer to an item in the model". Usually model indexes are shortlived, but sometimes you need to store such an index for future use. That's when you can use a persistent index. And how to use it? Simply assign a QModelIndex to it and use it as you'd use a regular model index.

defumar
18th January 2008, 11:11
A model index has nothing to do with database indexes. It's a "pointer to an item in the model". Usually model indexes are shortlived, but sometimes you need to store such an index for future use. That's when you can use a persistent index. And how to use it? Simply assign a QModelIndex to it and use it as you'd use a regular model index.
I think you've slightly misunderstood me. I know it's a pointer to an item which is persistent and it's got nothing to do with database indices but in what way is it persistent then. If my internal data structure changes (i.e. I'd reload the enumeration of all tasks in the Tasks table, all data would be deleted and reallocated), how could it possibly know that it should now point to row 4 instead of row 3 in my model as a new record has just been added knowing that the task's title is not unique and the model doesn't even load the record's database unique index.:)

wysota
18th January 2008, 15:40
I know it's a pointer to an item which is persistent and it's got nothing to do with database indices but in what way is it persistent then.
It will be updated when the model changes. Consider a situation that you have an index that points to an item in row=2 and column=0. Now you prepend a row to the model. Now the item that used to be in (2,0) coordinates has (3,0) coordinates. QModelIndex will still point to (2,0) whereas QPersistentModelIndex will point to (3,0).



If my internal data structure changes (i.e. I'd reload the enumeration of all tasks in the Tasks table, all data would be deleted and reallocated), how could it possibly know that it should now point to row 4 instead of row 3 in my model as a new record has just been added knowing that the task's title is not unique and the model doesn't even load the record's database unique index.:)

If you reset the model (for instance by calling QSqlQueryModel::select()) then all indexes will be invalidated. But if you use beginInsertRows() and endInsertRows() (or equivalents for deleting and for operating on columns), the model will update the persistent index accordingly. If some internal data changes, it shouldn't affect the persistent index, as it probably queries for the actual index only if you ask it to, thus it will return an index that is perfectly valid or one that is empty (invalid). But you can be sure that it won't point to a wrong element or contain an incorrect internalPointer or internalId.