Qt中的角色问题
在Qt中有些类是可以给他添加角色值的,比如说QComboBox中的setItemData()与QStandardItemModel中的setData()这两个函数,都是在Index位置上添加角色值。这两个函数原型如下:
void QComboBox::setItemData ( int index, const QVariant & value, int role = Qt::UserRole );
bool QStandardItemModel::setData ( const QModelIndex & index, const QVariant & value, int role = Qt::EditRole );
如果想在同一Index上添加多个角色值,我们可以利用下面的方法添加:
QStandardItemModel *pModel = new QStandardItemModel;
pModel->setData(pModel->index(0, 0), 1, Qt::UserRole);
pModel->setData(pModel->index(0, 0), 2, Qt::UserRole + 1);
这种方法多于表格处理,或者是QComboBox中需要获取其他信息。
在默认情况下,我们在利用这样的函数时我们并不加添加角色的,如果想利用这个功能我们就可以使用上面的方法来做。这里给大家提一个特例,QComboBox中的addItem()函数,他默认情况下是直接给Qt::UserRole这个角色添加值,而函数的原型中却没有给出这个角色,原型如下:
void QComboBox::addItem ( const QString & text, const QVariant & userData = QVariant() );
Adds an item to the combobox with the given text, and containing the specified userData (stored in the Qt::UserRole). The item is appended to the list of existing items.
enum Qt::ItemDataRole
Each item in the model has a set of data elements associated with it, each with its own role. The roles are used by the view to indicate to the model which type of data it needs. Custom models should return data in these types.
The general purpose roles (and the associated types) are:
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)
Roles describing appearance and meta data (with associated types):
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::AlignmentFlag)
Qt::BackgroundRole 8 The background brush used for items rendered with the default delegate. (QBrush)
Qt::BackgroundColorRole 8 This role is obsolete. Use BackgroundRole instead.
Qt::ForegroundRole 9 The foreground brush (text color, typically) used for items rendered with the default delegate. (QBrush)
Qt::TextColorRole 9 This role is obsolete. Use ForegroundRole instead.
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 (with associated types):
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.
For user roles, it is up to the developer to decide which types to use and ensure that components use the correct types when accessing and setting data.