TreeModel: hasIndex() vs. isValid()

  • Hi,

    I'm creating my first TreeModel as it is explained in THIS example.

    In the QModelIndex TreeModel::index(int iRow, int iColumn, const QModelIndex &parent)function, in a first step there is a check whether the given index does exist inside the TreeModel:

        if(!this->hasIndex(iRow, iColumn, parent))
            return QModelIndex();

    If this check returns true, then I think parent must be a valid index, otherwise the model would not return true when asking for the index, right?
    If so, then I'm asking myself why in the example there is a second check for validity of parent:

            parentItem = rootItem;
            parentItem = static_cast<myQTreeModelItem*>(parent.internalPointer());

    Is this second check really needed?

    Thank you in anticipation.

    Greetings, Binary

  • Moderators

    Top level items have no parent, so if an index is valid and its parent is not then this means the item is top level (root child in case of a tree structure). In other words in this example root is represented as an invalid index.

  • @Chris-Kawa Ah, I understand. Root index is invalid, I forgot that.

    Thank you!

  • Hi again,

    this topic was already marked as solved, but I need to know something else about QAbstractItemModel::hasIndex(int row, int column, const QModelIndex &parent) const:
    This function should check whether the given index exists in the model, right?
    When I'm checking the source code of this function in qabstractitemmodel.cpp, it only checks if the given row and column do exist inside the given model index:

        return row < rowCount(parent) && column < columnCount(parent);

    , but it does not check whether the model index itsself refers to the model, right?
    Shouldn't be there a check like:

        return row < rowCount(parent) && column < columnCount(parent) && parent.model == this;

    ? Otherwise I would still not know if the requested index was really part of my model or just part of the given index that may refer to another model...

  • Lifetime Qt Champion

    No since column/rowCount(parent) is using the correct model (the one from parent). Also QModelIndex() can be invalid.

  • @Christian-Ehrlicher This is the rowCount() implementation in the Tree Model example:

    int TreeModel::rowCount(const QModelIndex &parent) const
        TreeItem *parentItem;
        if (parent.column() > 0)
            return 0;
        if (!parent.isValid())
            parentItem = rootItem;
            parentItem = static_cast<TreeItem*>(parent.internalPointer());
        return parentItem->childCount();

    There is also no check whether the given model index refers to the "this" model or not. Right? So, if the given model index refers to an external model item that accidentally fits the given parameters (rows, columns), then the hasIndex function should return true, right? It does not check if the model is the current model or another one? Am I wrong?

  • Lifetime Qt Champion

    @Binary91-0 said in TreeModel: hasIndex() vs. isValid():

    Am I wrong?

    The function will return a maybe wrong value when you pass a wrong parameter, yes - you can add an assert in your implementations if your model is used from outside or if you don't trust yourself.

  • @Christian-Ehrlicher Alright, thank you, I just wanted to know if it was possible.

  • Lifetime Qt Champion

    It is possible and I think it's not checked since the programmer has to take care to not pass a wrong QModelIndex to the functions. The model is rarely checked, mostly only in SQFPM code due to this assumption.

  • Ok, I see. I think I need to get more further used to the whole topic to understand how the model works and what duties are on the programmer side and what should be handled by the model itsself.

    Thank you for the support.


Log in to reply