QModelIndex Class

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

Header: #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
(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 的项目模型的索引。项目视图、委托和选择模型使用该索引来定位模型中的项目。

新的 QModelIndex 对象由模型使用QAbstractItemModel::createIndex() 函数创建。无效的模型索引可通过 QModelIndex 构造函数构建。当引用模型中的顶层项时,无效索引通常被用作父索引。

模型索引指向模型中的项,并包含指定其在这些模型中位置所需的所有信息。每个索引都位于给定的行和列中,并且可能有一个父索引;使用row(),column(), 和parent() 可以获得这些信息。模型中的每个顶层项都由一个没有父索引的模型索引表示--在这种情况下,parent() 将返回一个无效的模型索引,相当于用 QModelIndex() 构造函数的零参数形式构造的索引。

要获取指向模型中现有项的模型索引,请调用QAbstractItemModel::index() 并输入所需的行和列的值以及父代的模型索引。当引用模型中的顶层项时,请提供 QModelIndex() 作为父索引。

model() 函数以QAbstractItemModel 的形式返回索引引用的模型。child() 函数用于检查模型中索引下的项目。sibling() 函数允许您在与索引相同的级别上遍历模型中的项目。

注意: 模型索引应立即使用,然后丢弃。在调用改变模型结构或删除项的模型函数后,不应依赖索引保持有效。如果需要长期保持模型索引,请使用QPersistentModelIndex

另请参阅 模型/视图编程QPersistentModelIndexQAbstractItemModel

成员函数文档

[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

一个有效的索引属于一个模型,并且行数和列数都是非负数。

另请参见 model()、row() 和column()。

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

返回指向模型的指针,该模型包含此索引指向的项目。

返回模型的常量指针是因为调用模型的非常量函数可能会使模型索引失效,并可能导致应用程序崩溃。

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

为索引指向的项目填充给定的roleDataSpan

此函数在 Qt 6.0 中引入。

QModelIndex QModelIndex::parent() const

返回模型索引的父节点,如果没有父节点,则返回QModelIndex() 。

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

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

返回此模型索引指向的行。

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

返回rowcolumn 的同级文件。如果该位置没有同级文件,则返回无效的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

与另一个模型索引比较时,将使用内部数据指针、行、列和模型值。

© 2025 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.