1

In my reimplementation of QStandardItem I am implementing setData. I understand how roles work in general, and their integer representation (see below for a full enumeration from the docs) , but am getting a role that is not in the usual enumeration: a role with constant 31. What is this mysterious undocumented role?

Here is my QStandardItem subclass (note this isn't something I am actually using, but something just to make this question more clear):

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)

Overall I see things that are reasonable, except I see role number 31 too printed out when I view my model:

setData for check state role
setData for other than editRole:  31

In the enumeration of the roles, I don't find role 31 (I cut/paste the entire list of roles, just in case I am being totally obtuse):

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.

Does anyone know what role 31 is?

eric
  • 7,142
  • 12
  • 72
  • 138

1 Answers1

2

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
};
ekhumoro
  • 115,249
  • 20
  • 229
  • 336
  • lol I love the comment for these weird roles: "Start worrying when public roles go that high." :) That of course brings up the question: why is there a whatsthis property being set for every row in my view? Weird....I still find the `whatsthis` one of the more mysterious features of Qt. – eric Apr 19 '15 at 17:51
  • 1
    @neuronet It might be something related to this: [`QWhatsThis`](http://doc.qt.io/qt-4.8/qwhatsthis.html) – Tay2510 Apr 20 '15 at 05:42
  • @Tay2510 yeah, I have asked about this before, just saying I still find it mysterious as it is one of those things that comes up a lot but is rarely used or discussed, for instance in the official Qt books: http://stackoverflow.com/questions/26975534/qdialog-how-to-use-question-mark-button – eric Apr 20 '15 at 11:42