Qt::UserRole这是什么意思啊??

DevinLai 2013-01-31 05:37:09
Qt::UserRole,这个UserRole,是什么意思??有人能用个具体的例子帮我解释下吗??
...全文
2696 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
奇趣科技 2013-02-01
  • 打赏
  • 举报
回复
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.
奇趣科技 2013-02-01
  • 打赏
  • 举报
回复
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.

65,186

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧