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 |
| 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 。用于项视图、委托及选定模型的索引,能定位模型项。
通过模型创建新 QModelIndex 对象是使用 QAbstractItemModel::createIndex () 函数。 无效 模型索引的构造可以采用 QModelIndex 构造函数。无效索引经常用作父级索引,当引用模型中的顶级项时。
模型索引引用模型中的项,且包含指定它们在这些模型中位置要求的所有信息。各索引位于给定行和列,且可能拥有父级索引;使用 row (), column (),和 parent () 能获得这种信息。模型中的各顶级项都是由不拥有父级索引的模型索引所表示的 - 在此情况下, parent () 将返回无效模型索引,相当于采用 QModelIndex() 构造函数的 0 自变量形式构造索引。
要获得引用模型中现有项的模型索引,调用 QAbstractItemModel::index () 采用要求的行和列值,及父级的模型索引。当引用模型中的顶级项时,提供 QModelIndex() 作为父级索引。
The model () 函数返回索引所引用的模型如 QAbstractItemModel 。child() 函数用于审查由模型索引所保持的项。 sibling () 函数允许在如索引的同一级别遍历模型项。
注意: 模型索引应立即使用,然后丢弃。不应依赖要保持有效的索引,在调用改变模型结构 (或删除项) 的模型函数后。若需要随时间推移保持模型索引,使用 QPersistentModelIndex .
另请参阅 模型/视图编程 , QPersistentModelIndex ,和 QAbstractItemModel .
[constexpr]
QModelIndex::
QModelIndex
()
创建新的空模型索引。这种类型的模型索引用于指示模型的位置无效。
另请参阅 isValid () 和 QAbstractItemModel .
[constexpr]
int
QModelIndex::
column
() const
返回此模型索引所引用的列。
返回
const void
*
指针用于模型,以将索引关联内部数据结构。
另请参阅 QAbstractItemModel::createIndex ().
返回数据为给定 role 对于索引所引用的项。
返回由索引所引用的项标志。
[constexpr]
quintptr
QModelIndex::
internalId
() const
返回
quintptr
用于模型,以将索引关联内部数据结构。
另请参阅 QAbstractItemModel::createIndex ().
返回
void
*
指针用于模型,以将索引关联内部数据结构。
另请参阅 QAbstractItemModel::createIndex ().
[constexpr]
bool
QModelIndex::
isValid
() const
返回
true
若此模型索引有效;否则返回
false
.
有效索引属于模型,且拥有非负行号和列号。
另请参阅 model (), row (),和 column ().
[constexpr]
const
QAbstractItemModel
*QModelIndex::
model
() const
返回包含此索引所引用项的模型指针。
返回模型 const 指针,因为调用模型的非 const 函数会使模型索引无效,并可能会使应用程序崩溃。
[since 6.0]
void
QModelIndex::
multiData
(
QModelRoleDataSpan
roleDataSpan
) const
Populates the given roleDataSpan 对于索引所引用的项。
该函数在 Qt 6.0 引入。
返回模型索引的父级,或 QModelIndex () 若它没有父级。
[constexpr]
int
QModelIndex::
row
() const
返回此模型索引所引用的行。
返回同级在 row and column 。若此位置没有同级,无效 QModelIndex 被返回。
另请参阅 parent (), siblingAtColumn (),和 siblingAtRow ().
返回同级在 column 对于当前行。若此位置没有同级,无效 QModelIndex 被返回。
另请参阅 sibling () 和 siblingAtRow ().
返回同级在 row 对于当前列。若此位置没有同级,无效 QModelIndex 被返回。
另请参阅 sibling () 和 siblingAtColumn ().
[constexpr]
bool
QModelIndex::
operator!=
(const
QModelIndex
&
other
) const
返回
true
若此模型索引未引用同一位置如
other
模型索引;否则返回
false
.
[constexpr]
bool
QModelIndex::
operator<
(const
QModelIndex
&
other
) const
返回
true
若此模型索引 <
other
模型索引;否则返回
false
.
< 计算对开发者并非直接有用 - 没有定义采用不同父级的索引比较方式。此运算符才存在,以便类可以用于 QMap .
[constexpr]
bool
QModelIndex::
operator==
(const
QModelIndex
&
other
) const
返回
true
若此模型索引引用同一位置如
other
模型索引;否则返回
false
.
使用内部数据指针、行、列及模型值,当比较另一模型索引时。
同义词 QList < QModelIndex >.