I am studying QT model / views, and here I’ll face some kind of problem, or rather one country with a function called:

bool QAbstractItemModel::hasIndex(int row, int column, const QModelIndex &parent = QModelIndex()) const 

which is used in the Qt example by constructing a simple tree model in a function:

 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); else return QModelIndex(); } 

I read in the Qt docks, for which the hasIndex function is intended - in short, to check for the existence of an index. But if the row and column are not equal to the row and column of the model index, then still hasIndex will return true.

enter image description here

So how should you properly use hasIndex ?

    1 answer 1

    Here is the implementation of this function:

     bool QAbstractItemModel::hasIndex(int row, int column, const QModelIndex &parent) const { if(row < 0 || column < 0) return false; return row < rowCount(parent) && column < columnCount(parent); } 

    I suppose explanations are superfluous?