PDA

View Full Version : QAbstractItemModel for QTreeView with separate data



clemens
15th June 2016, 14:41
Hello,
I have a problem implementing a QAbstractItemModel derived class. I might be missing fundamental concepts and missusing the Model/View concept, but first of all here is the situation:
The data is stored in a custom class. This data class is implemented completely independent from the view and the model and does not contain particular information about a certain tree structure. I now tried to implement the QAbstractItemModel in a way that it builds up the tree structure (basically in the parent and index function). I know how to build up the tree, given the data class and could describe it "with words". Tree relationships are for instance determined by the type of objects and the position of those in QLists that belong to the data class. In order to make this work, I have to somehow find out the type of the object that is referenced by the internal (void) pointer in a QModelIndex. I had to make some kind of virtual base class for those object because otherwise I was not able to find out the type (static/dynamic cast). Now I also want to have nodes in the TreeView that actually do not have any "real" data in the class. Those are used as categories/folders to structure things. The internal pointer of the corresponding QModelIndex could just point to some QString, but then I am not able to determine the type if I see this QModelIndex later on in the parent/index functions.
I have the feeling that I am missing some fundamental conceptional thing here. Perhaps my describtion is good enough and some expert can enlighten me ;)

Best
Clemens

anda_skoa
15th June 2016, 16:46
What you can do is to build and actual tree structure in the model, with two types of nodes

1) nodes that further point to data into your data structure
2) nodes that are categories

The interal point would then always be to such a node

The first kind of node could also hold the information you need to distinguish between different types in your data class.

Cheers,
_