Results 1 to 9 of 9

Thread: Storing shared pointer in QModelIndex

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Storing shared pointer in QModelIndex

    The "internal" stuff in a QModelIndex is internal to the respective model instance.
    I.e. any model function that gets a QModelIndex passed as an argument, can first check if model() is "this".
    If it is, it can work with either internalId() or internalPointer() knowing that itself and nobody else put that info into the model index.

    Also keep in mind that this lookup structure can be purged or at least cleaned in a lot of cases.

    Anything that modifies the model, be it setData() or inserting/removing rows, etc. is assumed to invalidate all model indexes.
    So any mapping that is kept for QModelndex -> internal data lookup could be invalidated at these times as well.
    (might have to check for persistent model indexes though).

    Event processing is also assumed to be invalidate model indexes, so one can also clear those mappings based on a timer.

    Cheers,
    _

  2. The following user says thank you to anda_skoa for this useful post:

    d_stranz (10th September 2015)

  3. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,315
    Thanks
    314
    Thanked 870 Times in 857 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Storing shared pointer in QModelIndex

    My QAbstractItemModel that wraps my C++ tree structure is intended to be read-only (i.e. no editing, display only). When the external tree changes, I do a reset on the model (and will clear the smart pointer map). So any model indexes stored as persistent indexes will go away.

    I guess I am interested in the more general case - what are best practices to use when mating an external data structure to a QAbstractItemModel? It seems that QStandardItemModel is about the only one that has built-in support for storing some reference to external user data into the model itself.

    The most complete example of a custom tree model I have found is from Summerfield's "Advanced Qt Programming", and he uses internalPointer() to store pointers to his tree items.

  4. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Storing shared pointer in QModelIndex

    The QStandardItemModel actually stores the data it serves.
    Usually this is not what you want, instead the QAIM subclass is used as an interface to the actual data.

    Yes, it is a pity that QModelIndex cannot hold a QVariant as its reference.
    But then again, a QVariant is basically just a void* plus type information and type information is what the model has implicitly since it knows what it puts into its indexes.
    The only missing bit is that the model doesn't know when the model index gets destroyed so it can't use this for cleanup triggering and has to resort to different means.

    Well, "only" because this is really annoying.

    Cheers,
    _

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

    d_stranz (11th September 2015)

Similar Threads

  1. Replies: 3
    Last Post: 23rd May 2015, 13:57
  2. Storing A hex value as a string?
    By Splatify in forum Newbie
    Replies: 4
    Last Post: 7th February 2011, 06:26
  3. Replies: 1
    Last Post: 4th December 2010, 17:20
  4. deleting internal pointer in QModelIndex
    By rickbsgu in forum Qt Programming
    Replies: 18
    Last Post: 24th December 2008, 02:24
  5. storing captions
    By vijay anandh in forum Qt Programming
    Replies: 3
    Last Post: 24th April 2006, 07:54

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
  •  
Qt is a trademark of The Qt Company.