Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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.


  • Qt Champions 2019

    @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.


  • Qt Champions 2019

    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 actual QModelIndex is created and returned by the use of createIndex(). 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 calling index() from outside the model and get a QModelIndex object. I imagine using that ModelIndexand call its parent() 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 to createIndex().
    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.