Results 1 to 5 of 5

Thread: how does QPersistentModelIndex "work"?

  1. #1
    Join Date
    Oct 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default how does QPersistentModelIndex "work"?

    I am struggling to understand QPersistentModelIndex. The documentation says that it allows me to "save" indexes. What does "save" mean... save to disk and reload when I run the program again? Or is it limited to the lifetime of the model?

    I note that there are some functions relating to persistent indexes in QAbstractItemModel; do I need to add persistent index support to my model by myself?

    In a nutshell, how do QPersistentModelIndexes "work"?

    Thanks,
    Dan.

  2. #2
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: how does QPersistentModelIndex "work"?

    It is unsafe to store QModelIndexes as for example class members, because they can become invalid. For example you have a delegate and in some special circumstances (on mose hover) you want to draw blue background. But when mouse cursor goes off this index to another, you have to determine which one was last to set its background to normal (I hope it is clear enough). So you have you delegate class with QModelIndex member. While painting blue background you store index in your member variable and in next call you are using this stored index to determine on which index you have to draw normal background (this last index where mouse was in) and now draw the blue background in current index. This solution is unsafe, as this stored QModelIndex might become invalid between calls, so you have to use QPersistentModelIndex which remains valid (ofcourse until you destroy model).
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  3. #3
    Join Date
    Oct 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how does QPersistentModelIndex "work"?

    I've dug into the source code and now everything makes perfect sense.

    My key misunderstanding is what persistant actually meant in this context. It means "persist across calls to the model, but not across model instance lifetimes." The documentation was not clear on this.

    Whenever an operation on the model is performed that would invalidate regular model indexes, the model will actively "fixup" all persistent model indexes so that they remain useable. Under the hood, the model calls index() to create updated indexes for the persistent indexes.

    faldzip, thank you for the nudge in the right direction.

    Dan.

  4. #4
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: how does QPersistentModelIndex "work"?

    But having persistent indexes slows down operations on model, so use them only if you really need.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: how does QPersistentModelIndex "work"?

    If you are using QSqlTableModel committing changes to the database (i.e. submitAll()) will invalidate persistent model indexes and possibly other model indexes.

Similar Threads

  1. The "Range" option in QHttpRequestHeader does not work?
    By nhs_0702 in forum Qt Programming
    Replies: 1
    Last Post: 19th April 2010, 10:36
  2. Replies: 0
    Last Post: 15th November 2009, 10:40
  3. "Follow symbol under cursor" does not work...?
    By liormessinger in forum Qt Tools
    Replies: 0
    Last Post: 16th September 2009, 18:22
  4. Translation QFileDialog standart buttons ("Open"/"Save"/"Cancel")
    By victor.yacovlev in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2008, 20:05
  5. Replies: 6
    Last Post: 3rd November 2006, 12:53

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.