QModelIndex 类

QModelIndex 类用于在数据模型中定位数据。 更多...

头: #include <QModelIndex>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core

公共函数

QModelIndex ()
int column () const
const void * constInternalPointer () const
QVariant data (int role = Qt::DisplayRole) const
Qt::ItemFlags flags () const
quintptr internalId () const
void * internalPointer () const
bool isValid () const
const QAbstractItemModel * model () const
(从 6.0 起) void multiData (QModelRoleDataSpan roleDataSpan ) const
QModelIndex parent () const
int row () const
QModelIndex sibling (int row , int column ) const
QModelIndex siblingAtColumn (int column ) const
QModelIndex siblingAtRow (int row ) const
bool operator!= (const QModelIndex & other ) const
bool operator< (const QModelIndex & other ) const
bool operator== (const QModelIndex & other ) const
QModelIndexList

详细描述

此类被用作项模型的索引,派生自 QAbstractItemModel . The index is used by item views, delegates, and selection models to locate an item in the model.

New QModelIndex objects are created by the model using the QAbstractItemModel::createIndex () function. An invalid model index can be constructed with the QModelIndex constructor. Invalid indexes are often used as parent indexes when referring to top-level items in a model.

Model indexes refer to items in models, and contain all the information required to specify their locations in those models. Each index is located in a given row and column, and may have a parent index; use row (), column (),和 parent () to obtain this information. Each top-level item in a model is represented by a model index that does not have a parent index - in this case, parent () will return an invalid model index, equivalent to an index constructed with the zero argument form of the QModelIndex() constructor.

To obtain a model index that refers to an existing item in a model, call QAbstractItemModel::index () with the required row and column values, and the model index of the parent. When referring to top-level items in a model, supply QModelIndex() as the parent index.

The model () function returns the model that the index references as a QAbstractItemModel . The child() function is used to examine items held under the index in the model. The sibling () function allows you to traverse items in the model on the same level as the index.

注意: Model indexes should be used immediately and then discarded. You should not rely on indexes to remain valid after calling model functions that change the structure of the model or delete items. If you need to keep a model index over time use a QPersistentModelIndex .

另请参阅 模型/视图编程 , QPersistentModelIndex ,和 QAbstractItemModel .

成员函数文档编制

[constexpr noexcept] 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.

另请参阅 isValid () 和 QAbstractItemModel .

[constexpr noexcept] int QModelIndex:: column () const

Returns the column this model index refers to.

[noexcept] const void *QModelIndex:: constInternalPointer () const

返回 const void * pointer used by the model to associate the index with the internal data structure.

另请参阅 QAbstractItemModel::createIndex ().

QVariant QModelIndex:: data ( int role = Qt::DisplayRole) const

返回数据为给定 role for the item referred to by the index.

Qt::ItemFlags QModelIndex:: flags () const

Returns the flags for the item referred to by the index.

[constexpr noexcept] quintptr QModelIndex:: internalId () const

返回 quintptr used by the model to associate the index with the internal data structure.

另请参阅 QAbstractItemModel::createIndex ().

[noexcept] void *QModelIndex:: internalPointer () const

返回 void * pointer used by the model to associate the index with the internal data structure.

另请参阅 QAbstractItemModel::createIndex ().

[constexpr noexcept] bool QModelIndex:: isValid () const

返回 true 若此模型索引有效;否则返回 false .

A valid index belongs to a model, and has non-negative row and column numbers.

另请参阅 model (), row (),和 column ().

[constexpr noexcept] const QAbstractItemModel *QModelIndex:: model () const

Returns a pointer to the model containing the item that this index refers to.

A const pointer to the model is returned because calls to non-const functions of the model might invalidate the model index and possibly crash your application.

[since 6.0] void QModelIndex:: multiData ( QModelRoleDataSpan roleDataSpan ) const

Populates the given roleDataSpan for the item referred to by the index.

该函数在 Qt 6.0 引入。

QModelIndex QModelIndex:: parent () const

Returns the parent of the model index, or QModelIndex () if it has no parent.

另请参阅 sibling () 和 model ().

[constexpr noexcept] int QModelIndex:: row () const

Returns the row this model index refers to.

QModelIndex QModelIndex:: sibling ( int row , int column ) const

返回同级在 row and column . If there is no sibling at this position, an invalid QModelIndex 被返回。

另请参阅 parent (), siblingAtColumn (),和 siblingAtRow ().

QModelIndex QModelIndex:: siblingAtColumn ( int column ) const

返回同级在 column for the current row. If there is no sibling at this position, an invalid QModelIndex 被返回。

另请参阅 sibling () 和 siblingAtRow ().

QModelIndex QModelIndex:: siblingAtRow ( int row ) const

返回同级在 row for the current column. If there is no sibling at this position, an invalid QModelIndex 被返回。

另请参阅 sibling () 和 siblingAtColumn ().

[constexpr noexcept] bool QModelIndex:: operator!= (const QModelIndex & other ) const

返回 true if this model index does not refer to the same location as the other model index; otherwise returns false .

[constexpr noexcept] bool QModelIndex:: operator< (const QModelIndex & other ) const

返回 true if this model index is smaller than the other model index; otherwise returns false .

The less than calculation is not directly useful to developers - the way that indexes with different parents compare is not defined. This operator only exists so that the class can be used with QMap .

[constexpr noexcept] bool QModelIndex:: operator== (const QModelIndex & other ) const

返回 true if this model index refers to the same location as the other model index; otherwise returns false .

The internal data pointer, row, column, and model values are used when comparing with another model index.

相关非成员

QModelIndexList

同义词 QList < QModelIndex >.