QModelIndex and related Classes in QML

Since Qt 5.5, QModelIndex and QPersistentModelIndex are exposed in QML as value-based types. Also exposed in a similar fashion are QModelIndexList, QItemSelectionRange and QItemSelection. All objects from these types can be passed back and forth between QML and C++ as var properties or plain JavaScript variables.

Below you will find an overview of the API exposed to QML for these classes. For more information, refer to their C++ documentation.

Note: Since all these types are exposed as gadgets, there are no property change notification signals emitted. Therefore binding to their properties may not give the expected results. This is especially true for QPersistentModelIndex.

QModelIndex and QPersistentModelIndex Types

All these properties are read-only, as are their C++ counterparts.

Note: The usual caveats apply to QModelIndex in QML. If the underlying model changes or gets deleted, it may become dangerous to access its properties. Therefore, you should not store any QModelIndex objects. You can, however, store QPersistentModelIndexe objects in a safe way.

QModelIndexList Type

QModelIndexList is exposed in QML as a JavaScript array. Conversions are automatically made from and to C++. In fact, any JavaScript array can be converted back to QModelIndexList, with non-QModelIndex objects replaced by invalid QModelIndexes.

Note: QModelIndex to QPersistentModelIndex conversion happens when accessing the array elements because any QModelIndexList property retains reference semantics when exposed this way.

QItemSelectionRange Type

All these properties are read-only, as are their C++ counterparts. In addition, we also expose the following functions:

QItemSelection Type

Similarly to QModelIndexList, QItemSelection is exposed in QML as a JavaScript array of QItemSelectionRange objects. Conversions are automatically made from and to C++. In fact, any JavaScript array can be converted back to QItemSelection, with non-QItemSelectionRange objects replaced by empty QItemSelectionRanges.

See also ItemSelectionModel.

© The Qt Company Ltd
Licensed under the GNU Free Documentation License, Version 1.3.
https://doc.qt.io/qt-6.0/qmodelindex-and-related-classes-in-qml.html