#include <qnamespace.h>
| enum ItemDataRole { |
| DisplayRole = 0, |
| DecorationRole = 1, |
| EditRole = 2, |
| ToolTipRole = 3, |
| StatusTipRole = 4, |
| WhatsThisRole = 5, |
| |
| FontRole = 6, |
| TextAlignmentRole = 7, |
| BackgroundColorRole = 8, |
| BackgroundRole = 8, |
| TextColorRole = 9, |
| ForegroundRole = 9, |
| CheckStateRole = 10, |
| |
| AccessibleTextRole = 11, |
| AccessibleDescriptionRole = 12, |
| |
| SizeHintRole = 13, |
| InitialSortOrderRole = 14, |
| |
| DisplayPropertyRole = 27, |
| DecorationPropertyRole = 28, |
| ToolTipPropertyRole = 29, |
| StatusTipPropertyRole = 30, |
| WhatsThisPropertyRole = 31, |
| |
| UserRole = 0x0100 |
| }; |
model中的每个item都有一组与之关联的数据元素,每个元素都有自己的role。view使用这些role向model指示它需要哪种类型的数据。自定义model应返回对应类型的数据。
general purpose roles
Constant |
value |
Description |
Qt::DisplayRole |
0 |
The key data to be rendered in the form of text. (QString) |
Qt::DecorationRole |
1 |
The data to be rendered as a decoration in the form of an icon. (QColor, QIcon or QPixmap) |
Qt::EditRole |
2 |
The data in a form suitable for editing in an editor. (QString) |
Qt::ToolTipRole |
3 |
The data displayed in the item's tooltip. (QString) |
Qt::StatusTipRole |
4 |
The data displayed in the status bar. (QString) |
Qt::WhatsThisRole |
5 |
The data displayed for the item in "What's This?" mode. (QString) |
Qt::SizeHintRole |
13 |
The size hint for the item that will be supplied to views. (QSize) |
Constant |
value |
Description |
Qt::FontRole |
6 |
The font used for items rendered with the default delegate. (QFont) |
Qt::TextAlignmentRole |
7 |
The alignment of the text for items rendered with the default delegate. (Qt::Alignment) |
Qt::BackgroundRole |
8 |
The background brush used for items rendered with the default delegate. (QBrush) |
Qt::BackgroundColorRole |
8 |
expired, use Qt::BackgroundRole |
Qt::ForegroundRole |
9 |
The foreground brush (text color, typically) used for items rendered with the default delegate. (QBrush) |
Qt::TextColorRole |
9 |
expired, use Qt::ForegroundRole |
Qt::CheckStateRole |
10 |
This role is used to obtain the checked state of an item. (Qt::CheckState) |
Qt::InitialSortOrderRole |
14 |
This role is used to obtain the initial sort order of a header view section. (Qt::SortOrder). This role was introduced in Qt 4.8 |
Accessibility roles
constant |
value |
Description |
Qt::AccessibleTextRole |
11 |
The text to be used by accessibility extensions and plugins, such as screen readers. (QString) |
Qt::AccessibleDescriptionRole |
12 |
A description of the item for accessibility purposes. (QString) |
User roles
constant |
value |
Description |
Qt::UserRole |
0x0100 |
The first role that can be used for application-specific purposes |
code
| enum CustomRole{ |
| NAME = Qt::UserRole, |
| AGE |
| }; |
| |
| setData(QString("abc"), NAME); |
| data(NAME); |
| |
| |
| if (Qt::DisplayRole == role){return QString();} |
| else if (Qt::TextColorRole == role) {return QColor(255, 0, 0);} |
| else if (Qt::ToolTipRole == role){ return QString();} |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异