QModelIndex and related Classes in QML

从 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.

注意: 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.

注意: 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 QModelIndex es.

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

QItemSelectionRange 类型

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

QItemSelection Type

类似于 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 QItemSelectionRange

另请参阅 ItemSelectionModel .