PDA

View Full Version : Inter-related QStandardItemModel



koosh
2nd July 2008, 05:59
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.

wysota
2nd July 2008, 08:21
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.

koosh
2nd July 2008, 10:21
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.

wysota
2nd July 2008, 12:49
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.

koosh
2nd July 2008, 21:36
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*)

wysota
2nd July 2008, 22:34
What exactly do you consider a "related item"? Maybe we are misunderstanding each other...

koosh
2nd July 2008, 23:22
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.

wysota
2nd July 2008, 23:26
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.

koosh
2nd July 2008, 23:42
OK, I guess I'll just play around with it until I figure out what seems to work best. Thanks for your input.