QModelIndex Class
QModelIndex 类用于在数据模型中定位数据。更多内容...
| 头文件: | #include <QModelIndex> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS Core) target_link_libraries(mytarget PRIVATE Qt6::Core) |
| qmake: | QT += core |
QModelIndex 比较
| 类别 | 可比较类型 |
|---|---|
| strong | QModelIndex |
公共函数
| 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 |
(since 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 |
相关的非成员
| QModelIndexList | |
| bool | operator!=(const QModelIndex &lhs, const QModelIndex &rhs) |
| bool | operator<(const QModelIndex &lhs, const QModelIndex &rhs) |
| bool | operator==(const QModelIndex &lhs, const QModelIndex &rhs) |
详细说明
该类用作从QAbstractItemModel 派生的项目模型的索引。项目视图、委托和选择模型使用该索引在模型中定位项目。
模型通过调用QAbstractItemModel::createIndex()函数来创建新的QModelIndex对象。可以使用QModelIndex构造函数构建一个无效的模型索引。在引用模型中的顶级项目时,无效索引通常被用作父索引。
模型索引指向模型中的项目,并包含指定其在模型中位置所需的所有信息。每个索引位于指定的行和列中,并可能有一个父索引;可使用row()、column()和parent()来获取这些信息。 模型中的每个顶级项目都由一个没有父索引的模型索引表示——在这种情况下,parent() 将返回一个无效的模型索引,该索引等同于使用 QModelIndex() 构造函数的零参数形式构建的索引。
要获取指向模型中现有项的模型索引,请调用QAbstractItemModel::index(),并传入所需的行和列值以及父索引。当引用模型中的顶级项时,请将 QModelIndex() 作为父索引传入。
model() 函数会将该索引所引用的模型作为QAbstractItemModel 返回。child() 函数用于检查模型中该索引下所包含的项目。sibling() 函数允许您遍历与该索引处于同一层级的模型项目。
注意:模型 索引应立即使用并随后丢弃。在调用会改变模型结构或删除项的模型函数后,不应依赖索引仍保持有效。若需长期保留模型索引,请使用 `QPersistentModelIndex`。
另请参阅 《模型/视图编程》、《QPersistentModelIndex 》以及《QAbstractItemModel 》。
成员函数文档
[constexpr noexcept] QModelIndex::QModelIndex()
创建一个新的空模型索引。此类模型索引用于指示模型中的该位置无效。
另请参阅 isValid() 和QAbstractItemModel 。
[constexpr noexcept] int QModelIndex::column() const
返回该模型索引所指的列。
[noexcept] const void *QModelIndex::constInternalPointer() const
返回一个const void * 指针,该指针由模型用于将索引与内部数据结构关联起来。
另请参阅 QAbstractItemModel::createIndex()。
QVariant QModelIndex::data(int role = Qt::DisplayRole) const
返回给定role 中由索引所指项目的数据;如果该模型索引为invalid ,则返回一个默认构造的QVariant 。
Qt::ItemFlags QModelIndex::flags() const
返回索引所指向的项目的标志。
[constexpr noexcept] quintptr QModelIndex::internalId() const
返回一个quintptr 对象,该对象由模型用于将索引与内部数据结构建立关联。
另请参阅 QAbstractItemModel::createIndex()。
[noexcept] void *QModelIndex::internalPointer() const
返回一个void * 指针,该指针由模型用于将索引与内部数据结构关联起来。
另请参阅 QAbstractItemModel::createIndex()。
[constexpr noexcept] bool QModelIndex::isValid() const
如果该模型索引有效,则返回true ;否则返回false 。
有效的索引属于某个模型,且其行号和列号均为非负数。
[constexpr noexcept] const QAbstractItemModel *QModelIndex::model() const
返回指向包含该索引所指项的模型的指针。
返回的是该模型的 const 指针,因为调用该模型的非 const 函数可能会使模型索引失效,从而可能导致应用程序崩溃。
[since 6.0] void QModelIndex::multiData(QModelRoleDataSpan roleDataSpan) const
为索引所指向的项目填充指定的roleDataSpan 。
该函数于 Qt 6.0 中引入。
QModelIndex QModelIndex::parent() const
返回模型索引的父节点;如果该索引没有父节点,则返回QModelIndex()。
[constexpr noexcept] int QModelIndex::row() const
返回该模型索引所指的行。
QModelIndex QModelIndex::sibling(int row, int column) const
返回位于row 和column 的同级元素。如果该位置不存在同级元素,则返回一个无效的QModelIndex 。
另请参阅 parent()、siblingAtColumn() 和siblingAtRow()。
QModelIndex QModelIndex::siblingAtColumn(int column) const
返回当前行在column 位置处的同级元素。如果该位置没有同级元素,则返回一个无效的QModelIndex 。
另请参阅 sibling() 和siblingAtRow()。
QModelIndex QModelIndex::siblingAtRow(int row) const
返回当前列在row 处的兄弟节点。如果该位置不存在兄弟节点,则返回一个无效的QModelIndex 。
另请参阅 sibling() 和siblingAtColumn()。
相关的非成员方法
QModelIndexList
QList<QModelIndex> 的同义词。
[constexpr noexcept] bool operator!=(const QModelIndex &lhs, const QModelIndex &rhs)
如果lhs 模型索引与rhs 模型索引指向的位置不同,则返回true ;否则返回false 。
[constexpr noexcept] bool operator<(const QModelIndex &lhs, const QModelIndex &rhs)
如果lhs 模型索引小于rhs 模型索引,则返回true ;否则返回false 。
“小于”运算对开发者而言并无直接用处——具有不同父节点的索引之间如何比较并未定义。该运算符的存在仅是为了使该类能够与QMap 配合使用。
[constexpr noexcept] bool operator==(const QModelIndex &lhs, const QModelIndex &rhs)
如果lhs 模型索引与rhs 模型索引指向同一位置,则返回true ;否则返回false 。
在与另一个模型索引进行比较时,将使用内部数据指针、行、列和模型值。
© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.