How does createIndex know about parent
-
I'm trying to understant QAbstractItemModel::createIndex(). I looked into the Simple Tree Model example and if I understand correctly I feed it with the row, the column and a pointer to my internal data structure for the item in question.
What I cannot wrap my head around is the following: As a QModelIndex consists of row, column and parent Index, how does the newly created Index know about the parent index when I don't pass one as parameter?
-
@tonnerkiller OK, some more reading in the docs lead me to the solution of my problem:
QModelIndex does not itself know about the parent index of an item, but of course it knows about the model. So obviously QModelIndex, when queried about the parent index of its item will just call the model's parent() method.Of course the model's parent() method cannot rely on the passed QModelIndex's parent() method, but needs to go over the pointer to the internal item, so the internal item should be organised in a way that its parent is always clear to the model.
Sorry for bothering, I just should have read more at the right place.
-
@tonnerkiller said in How does createIndex know about parent:
I don't pass one as parameter?
Then you're using it wrong.
-
@Christian-Ehrlicher Where would I pass it? As I see it I pass row, column and a pointer to the item. Nothing to the parent. At least this is what I see in simle tree example and what I read in the docs. There is no pointer to the parent index, only to the internal item.
-
You (= the model) must know the stuff so the internal pointer points to your tree data structure in the common case. Therefore you know the parent.
-
Well, yes, I, that is the model, does know about its internals. But still I do not understand (please bear with me).
Let me rephrase and be a bit more verbose:
The docs say:Each index is located in a given row and column, and may have a parent index
So I expect the
QModelIndex
to know about these things: row, column, parent index.Now I tried to understand more by looking at the Simple Tree Model Exampel. There I find the following Code for the index method:
QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) const { if (!hasIndex(row, column, parent)) return QModelIndex(); TreeItem *parentItem; if (!parent.isValid()) parentItem = rootItem; else parentItem = static_cast<TreeItem*>(parent.internalPointer()); TreeItem *childItem = parentItem->child(row); if (childItem) return createIndex(row, column, childItem); return QModelIndex(); }
I pass into it a column, a row and the pointer to the parent index to have it create me a
QModelIndex
. Now these parameters are used in the method to determine the demanded internal data. I understand that.
Where I have problems following is when the actualQModelIndex
is created and returned by the use ofcreateIndex()
. It is passed the row, the column and the pointer to the internal data, but it is not passed any info on the parent of the item, only row and column.
Now I imagine callingindex()
from outside the model and get aQModelIndex
object. I imagine using thatModelIndex
and call itsparent()
method. How would the index object know about it? It has never been given this info,right?
I also found no method in the docs that would allow me to set the parent afterwards, so I guess it must be set at creation, but still it is not one of the parameters passed tocreateIndex()
.
Or do I miss something completely (I guess it is so because I have not seen this question asked before so I'm maybe the only one with this problem comprehending).
So please if you have any clue what my misunderstanding is and can explain me how things really work, help me.Thanks a lot.
-
@tonnerkiller OK, some more reading in the docs lead me to the solution of my problem:
QModelIndex does not itself know about the parent index of an item, but of course it knows about the model. So obviously QModelIndex, when queried about the parent index of its item will just call the model's parent() method.Of course the model's parent() method cannot rely on the passed QModelIndex's parent() method, but needs to go over the pointer to the internal item, so the internal item should be organised in a way that its parent is always clear to the model.
Sorry for bothering, I just should have read more at the right place.
-
For the record, this was a perfectly good question. The QAbstractItemModel::createIndex() docs do not state that it embeds a model pointer into the index it creates, nor do the QModelIndex docs mention the private constructor that makes it possible.
This makes QModelIndex::parent() look suspicious if you happen to be troubleshooting a problem. If you want to be sure your model is implemented correctly, you have to put aside the docs and dig up the inline function definitions in qabstractitemmodel.h.