2
votes

I am reading through following documentation to increase my understanding of model-view programming:

http://qt-project.org/doc/qt-4.8/model-view-programming.html

Although its nicely written, I have a question regarding creating indexes. As far as I understand to calculate and item's index we need index of its parent item. To calculate index of the parent item we need index of child item. So its a circular dependency. How do you break it?

If you go through following section:

Rows and columns

The diagram shows a representation of a basic table model in which each item is located by a pair of row and column numbers. We obtain a model index that refers to an item of data by passing the relevant row and column numbers to the model.

 QModelIndex indexA = model->index(0, 0, QModelIndex());
 QModelIndex indexB = model->index(1, 1, QModelIndex());
 QModelIndex indexC = model->index(2, 1, QModelIndex());

Top level items in a model are always referenced by specifying QModelIndex() as their parent item. This is discussed in the next section.

I do not understand how does Qt know to calculate index of A, parent index should be QModelIndex() (i.e. the index of top level item).

1
"how does Qt know to calculate index of A, parent index should be QModelIndex()" It is. - cmannett85

1 Answers

0
votes

As far as I understand to calculate and item's index we need index of its parent item.

Yes, unless it is a top level item.

To calculate index of the parent item we need index of child item.

No. An index cannot have more than one parent, so just call QModelIndex::parent() const.