Qt::UserRole

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.

posted on 2017-10-30 16:23  小舟翻动水中天  阅读(1283)  评论(0编辑  收藏  举报

导航