本页内容

QPersistentModelIndex Class

QPersistentModelIndex 类用于在数据模型中定位数据。更多内容...

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

QPersistentModelIndex 比较

类别可比较类型
强QPersistentModelIndex
strongQModelIndex.

公共函数

QPersistentModelIndex(const QModelIndex &index)
QPersistentModelIndex(const QPersistentModelIndex &other)
QPersistentModelIndex(QPersistentModelIndex &&other)
int column() const
QVariant data(int role = Qt::DisplayRole) const
Qt::ItemFlags flags() 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
void swap(QPersistentModelIndex &other)
operator QModelIndex() const
QPersistentModelIndex &operator=(QPersistentModelIndex &&other)
QPersistentModelIndex &operator=(const QModelIndex &other)
QPersistentModelIndex &operator=(const QPersistentModelIndex &other)
size_t qHash(const QPersistentModelIndex &key, size_t seed = 0)
bool operator!=(const QPersistentModelIndex &lhs, const QModelIndex &rhs)
bool operator!=(const QPersistentModelIndex &lhs, const QPersistentModelIndex &rhs)
bool operator<(const QPersistentModelIndex &lhs, const QPersistentModelIndex &rhs)
bool operator==(const QPersistentModelIndex &lhs, const QModelIndex &rhs)
bool operator==(const QPersistentModelIndex &lhs, const QPersistentModelIndex &rhs)

详细说明

QPersistentModelIndex 是一种模型索引,可由应用程序存储,并随后用于访问模型中的信息。与QModelIndex 类不同,存储 QPersistentModelIndex 是安全的,因为只要模型能够访问这些项目,模型就会确保对项目的引用始终有效。

在使用持久化模型索引之前,应先检查其有效性,这是良好的编程实践。

注意:您 无法将QStandardItemModel 的QPersistentModelIndex存储在模型的某个项目中。

另请参阅 “模型/视图编程”、“QModelIndex ”和“QAbstractItemModel ”。

成员函数文档

QPersistentModelIndex::QPersistentModelIndex(const QModelIndex &index)

创建一个新的 QPersistentModelIndex,该对象是模型index 的副本。

QPersistentModelIndex::QPersistentModelIndex(const QPersistentModelIndex &other)

创建一个新的 QPersistentModelIndex,该对象是other 持久化模型索引的副本。

[noexcept] QPersistentModelIndex::QPersistentModelIndex(QPersistentModelIndex &&other)

通过Move操作构造一个QPersistentModelIndex实例,使其指向与other 所指向的相同对象。

int QPersistentModelIndex::column() const

返回该持久化模型索引所引用的列。

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

返回给定role 中由索引所指项的数据,或者如果该持久化模型索引为invalid ,则返回一个默认构造的QVariant 。

另请参阅 Qt::ItemDataRole 和QAbstractItemModel::setData()。

Qt::ItemFlags QPersistentModelIndex::flags() const

返回索引所指向的项目的标志。

bool QPersistentModelIndex::isValid() const

如果该持久化模型索引有效,则返回true ;否则返回false 。

有效的索引属于某个模型,且其行号和列号均为非负数。

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

const QAbstractItemModel *QPersistentModelIndex::model() const

返回该索引所属的模型。

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

为索引所指的项填充指定的roleDataSpan 。

该函数于 Qt 6.0 中引入。

另请参阅 Qt::ItemDataRole 和QAbstractItemModel::setData()。

QModelIndex QPersistentModelIndex::parent() const

返回此持久化索引的父索引QModelIndex ;如果该索引没有父索引,则返回无效的QModelIndex 。

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

int QPersistentModelIndex::row() const

返回该持久化模型索引所指的行。

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

返回位于row 和column 处的同级节点,如果该位置不存在同级节点,则返回无效的QModelIndex 。

另请参阅 parent()。

[noexcept] void QPersistentModelIndex::swap(QPersistentModelIndex &other)

将此持久化模型索引与other 进行交换。此操作速度极快,且绝不会失败。

QPersistentModelIndex::operator QModelIndex() const

一个返回QModelIndex 的强制类型转换运算符。

[noexcept] QPersistentModelIndex &QPersistentModelIndex::operator=(QPersistentModelIndex &&other)

将other 通过Move操作赋值给该QPersistentModelIndex 实例。

QPersistentModelIndex &QPersistentModelIndex::operator=(const QModelIndex &other)

将持久化模型索引设置为指向模型中与other 模型索引相同的项。

QPersistentModelIndex &QPersistentModelIndex::operator=(const QPersistentModelIndex &other)

将持久化模型索引设置为指向模型中与other 持久化模型索引相同的项。

相关非成员

[noexcept] size_t qHash(const QPersistentModelIndex &key, size_t seed = 0)

返回key 的哈希值,并使用seed 作为计算的种子。

[noexcept] bool operator!=(const QPersistentModelIndex &lhs, const QModelIndex &rhs)

如果lhs 中的持久化模型索引与rhs 中的模型索引指向的位置不同,则返回true ;否则返回false 。

[noexcept] bool operator!=(const QPersistentModelIndex &lhs, const QPersistentModelIndex &rhs)

如果lhs 的持久化模型索引与rhs 的持久化模型索引不一致,则返回true ;否则返回false 。

[noexcept] bool operator<(const QPersistentModelIndex &lhs, const QPersistentModelIndex &rhs)

如果lhs 持久化模型索引小于rhs 持久化模型索引,则返回true ;否则返回false 。

在与另一个持久化模型索引进行比较时,将使用持久化模型索引中的内部数据指针、行、列和模型值。

[noexcept] bool operator==(const QPersistentModelIndex &lhs, const QModelIndex &rhs)

如果lhs 持久化模型索引与rhs 模型索引指向同一位置,则返回true ;否则返回false 。

在与另一个模型索引进行比较时,将使用持久化模型索引中的内部数据指针、行、列和模型值。

[noexcept] bool operator==(const QPersistentModelIndex &lhs, const QPersistentModelIndex &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.