在我QStandardItem
的重新实现中,我正在实施setData
。我理解角色一般是如何工作的,以及它们的整数表示(请参阅下面的文档中的完整枚举),但我得到的角色不是通常的枚举:具有常量 31 的角色。什么是这个神秘的无证件角色?
这是我的QStandardItem
子类(注意这不是我实际使用的东西,但只是为了使这个问题更清楚):
class StandardItem(QtGui.QStandardItem):
def setData(self, newValue, role=QtCore.Qt.UserRole + 1):
if role == QtCore.Qt.EditRole:
print "setData for editRole"
QtGui.QStandardItem.setData(self, newValue, role)
return True
if role == QtCore.Qt.CheckStateRole:
print "setData for check state role"
QtGui.QStandardItem.setData(self, newValue, role)
return True
print "setData for other cases, with role ", role
QtGui.QStandardItem.setData(self, newValue, role)
总的来说,我看到一些合理的事情,除了我在查看我的模型时看到的字母编号31也被打印出来了:
setData for check state role
setData for other than editRole: 31
在enumeration of the roles中,我找不到角色31(我剪切/粘贴了整个角色列表,以防万一我完全无视):
Role Constant Desc
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)
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.
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)
Qt.UserRole 32 The first role that can be used for application-specific purposes.
有谁知道31是什么角色?
答案 0 :(得分:1)
src/corelib/global/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 = 32
};