#include <qnamespace.h>
enum ItemDataRole {
DisplayRole = 0,
DecorationRole = 1,
EditRole = 2,
ToolTipRole = 3,
StatusTipRole = 4,
WhatsThisRole = 5,
// Metadata
FontRole = 6,
TextAlignmentRole = 7,
BackgroundColorRole = 8,
BackgroundRole = 8,
TextColorRole = 9,
ForegroundRole = 9,
CheckStateRole = 10,
// Accessibility
AccessibleTextRole = 11,
AccessibleDescriptionRole = 12,
// More general purpose
SizeHintRole = 13,
InitialSortOrderRole = 14,
// Internal UiLib roles. Start worrying when public roles go that high.
DisplayPropertyRole = 27,
DecorationPropertyRole = 28,
ToolTipPropertyRole = 29,
StatusTipPropertyRole = 30,
WhatsThisPropertyRole = 31,
// Reserved
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();}