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
- row : int
- column : int
- parent : QModelIndex
- valid : bool
- model : QAbstractItemModel
- internalId : quint64
All these properties are read-only, as are their C++ counterparts. In addition, we also expose the following functions:
- QVariant data(int role) (since Qt 6.7)
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
- top : int
- left : int
- bottom : int
- right : int
- width : int
- height : int
- topLeft : QPersistentModelIndex
- bottomRight : QPersistentModelIndex
- parent : QModelIndex
- valid : bool
- empty : bool
- model : QAbstractItemModel
All these properties are read-only, as are their C++ counterparts. In addition, we also expose the following functions:
- bool contains(QModelIndex index)
- bool contains(int row, int column, QModelIndex parentIndex)
- bool intersects(QItemSelectionRange other)
- QItemSelectionRange intersected(QItemSelectionRange other)
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.