c++qtqstandarditemmodelqmodelindex

How to create an invalid QModelIndex?


I am implementing a function which searchs a QModelIndex based on a QString input. However if I cannot find this index, the function should retun an invalid index. My model is a QStandardItemModel.

Is is okay to return invisibleRootItem()->index() as an invalid index? In the qt documentation is noted that it will always creates an invalid index.


Solution

  • You must use the default constructor of QModelIndex(e.g. return QModelIndex()) which is null as indicated in the docs:

    QModelIndex::QModelIndex()
    Creates a new empty model index. This type of model index is used to indicate that the position in the model is invalid.

    (emphasis mine)