本页内容

QModelRoleData Class

QModelRoleData 类用于存储一个角色及其相关数据。更多内容...

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

公共函数

QModelRoleData(int role)
void clearData()
QVariant &data()
const QVariant &data() const
int role() const
void setData(T &&value)

详细说明

QModelRoleData 对象存储一个项角色(该角色是Qt::ItemDataRole 枚举中的一个值,或是自定义角色的任意整数),以及与该角色相关的数据。

QModelRoleData 对象通常由视图或委托创建,并设置它们要获取数据的角色。然后,该对象被传递给模型(参见QAbstractItemModel::multiData()),模型会填充与存储的角色相对应的数据。最后,视图将从模型中检索到的数据可视化。

另请参阅 “模型/视图编程”和“QModelRoleDataSpan ”。

成员函数文档

[explicit noexcept] QModelRoleData::QModelRoleData(int role)

根据给定的role 构建一个QModelRoleData对象。

另请参阅 Qt::ItemDataRole 。

[noexcept] void QModelRoleData::clearData()

清除该对象所包含的数据。请注意,角色保持不变;仅数据被清除。

另请参阅 data()。

[constexpr noexcept] QVariant &QModelRoleData::data()

将该对象所包含的数据作为可修改引用返回。

另请参阅 setData()。

[constexpr noexcept] const QVariant &QModelRoleData::data() const

返回该对象所包含的数据。

另请参阅 ` setData()`。

[constexpr noexcept] int QModelRoleData::role() const

返回该对象所拥有的角色。

另请参阅 Qt::ItemDataRole 。

[constexpr noexcept(...)] template <typename T> void QModelRoleData::setData(T &&value)

将该对象所包含的数据设置为value 。value 必须是可存储在QVariant 中的数据类型。

注意: 当noexcept(m_data.setValue(std::forward<T>(value))) 为true 时,此 函数为noexcept。

另请参阅 data()、clearData() 和Q_DECLARE_METATYPE 。

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