Results 1 to 9 of 9

Thread: Inter-related QStandardItemModel

  1. #1
    Join Date
    Jun 2008
    Posts
    17
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Inter-related QStandardItemModel

    I'm sure this question has been asked before, but I couldn't find an example of it.

    My model stores CAD data. It's arranged hierarchically, and besides storing data in the rows, a given item needs to be associated with row(s) in other parts of the model. What's the best way to implement this?

    I was planning on using item.setData(ptr, UserRole) to store a pointer to the associated QStandardItem. The QVariant doesn't cast to a StandardItem, though. Should I subclass QStandardItem and add a method to store my pointer data? Or is there a more standardized way of doing this? I assume using QModelIndex is out of the question, as the model will change.

    Thanks.

  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: Inter-related QStandardItemModel

    There are two choices

    1. subclass QStandardItem and extend it with your structure
    2. implement your own model and use something called an internal pointer

    Whichever way you choose, first think if it is a good idea to expose the pointer. It shouldn't be needed outside the model - ever. Instead you should be using custom roles to expose every piece of data from your structure.

  3. #3
    Join Date
    Jun 2008
    Posts
    17
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Inter-related QStandardItemModel

    I suppose ideally, i'd want to get a ModelIndex to the related item, not exposing any pointers outside the model.

    Originally I thought about storing a QPersistantModelIndex in a custom role, to point to the related items, but I'm worried about the performance issue of this kind of 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: Inter-related QStandardItemModel

    Quote Originally Posted by koosh View Post
    I suppose ideally, i'd want to get a ModelIndex to the related item, not exposing any pointers outside the model.
    You can query for the index if you have the row, column and parent.

    Originally I thought about storing a QPersistantModelIndex in a custom role, to point to the related items, but I'm worried about the performance issue of this kind of index.
    Bad idea. You want to store an index in an index? What for? If you have a structure, either wrap the model around it or map it to standard item model.

  5. #5
    Join Date
    Jun 2008
    Posts
    17
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Inter-related QStandardItemModel

    I'm still trying to get my head around this.... I don't want to store and index inside an index. I simply want to store some kind of handle to a related item inside of a given item.

    You recommended using custom roles, as I originally wanted to do. Reading about QMetaType and QVariant, I think that I could store a custom role data which is a QVariant cast of a QStandardItem. Does this make sense? That way I use the custom roles and don't have to subclass QStandardItem. To obtain the related item, I'll do something like:

    QStandardItem *relatedItem = item.data(CustomRole);
    If I'm reading this right, I'll need Q_DECLARE_METATYPE(QStandardItem*)

  6. #6
    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: Inter-related QStandardItemModel

    What exactly do you consider a "related item"? Maybe we are misunderstanding each other...

  7. #7
    Join Date
    Jun 2008
    Posts
    17
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Inter-related QStandardItemModel

    I have a hierarchical table, where child items are grouped by their type (each one is a CAD primitive object). Children under one parent have a relationship to children under another parent. I need to store this relationship somehow in the model, but I don't want to duplicate data. The data is viewed in several Tree and Table views and also a custom graphic view, so I began with QStandardItem. I was planning to store the CAD data in each standard item as the DisplayRole, and also a handle to the 'related' StandardItem as UserRoles.

    The Qt model seems very flexible, and I'm just trying to figure the best way to do this.

  8. #8
    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: Inter-related QStandardItemModel

    If your objects have some kind of ID, you can store an ID in a custom role and provide a custom method for getting an index of an item with a given id.

    I'm not sure what is the exact relation and hierarchy between items, so it's hard for me to suggest something better.

  9. #9
    Join Date
    Jun 2008
    Posts
    17
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Inter-related QStandardItemModel

    OK, I guess I'll just play around with it until I figure out what seems to work best. Thanks for your input.

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.