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)

将此对象持有的数据设置为valuevalue 必须是可存储在QVariant 中的数据类型。

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

另请参阅 data(),clearData() 和Q_DECLARE_METATYPE

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