View Full Version : Accessing parent IndexModel in a tree Model

30th April 2013, 06:38

Is there any difference in accessing indexes' parents through the indexes themselves, with QModelIndex:: parent(), or through the model they belong to, with QAbstractItemModel:: parent(const QModelIndex & index)?
I have a hard time understanding which of the model or the the indexes should keeep the tree hierachy information?

Also, since QAbstractItemModel::createIndex(int row, int column, void * ptr = 0) does not offer to specify a parent index when creating a new index, how does an QModelIndex know what parent index it belongs to using QModelIndex:: parent()?


30th April 2013, 08:31
QModelIndex::parent() is implemented using QAbstractItemModel::parent() anyway. QModelIndex keeps a pointer to the model to facilitate this.

QAbstractItemModel::createIndex() is used internally to a model to generate indexes for items. The pointer or integer data member is often used as a pointer to the internal data structure that represents the item. That model's implementation of parent() can use the internal pointer to find the item representing the index, and from that derive the row QModelIndex of the parent. For a worked example see Ch4 in Advanced Qt Programming by Summerfield.