Results 1 to 4 of 4

Thread: QPersistentModelIndex usage info request

  1. #1
    Join Date
    Jan 2008
    Posts
    21
    Thanks
    3

    Default QPersistentModelIndex usage info request

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QPersistentModelIndex usage info request

    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.

  3. #3
    Join Date
    Jan 2008
    Posts
    21
    Thanks
    3

    Default Re: QPersistentModelIndex usage info request

    Quote Originally Posted by wysota View Post
    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QPersistentModelIndex usage info request

    Quote Originally Posted by defumar View Post
    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.

  5. The following user says thank you to wysota for this useful post:

    defumar (18th January 2008)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.