3

In a QTableModel when I enter the following:

print model.supportedDropActions()

I just get:

<PyQt4.QtCore.DropActions object at 0x00000000081172E8>

How can I access an actual list of the supported drop actions from this object? At the documentation, it says, "The DropActions type is a typedef for QFlags. It stores an OR combination of DropAction values."

Note I am doing this in Python (PySide).

Related posts:

Community
  • 1
  • 1
eric
  • 7,142
  • 12
  • 72
  • 138
  • As the docs say, it's just OR'd together values, so convert it to an int. – ekhumoro Oct 12 '14 at 15:40
  • @ekhumoro right, so it is just like any other flag, no? OK I can work with that. One thing I am confused about is they seem to be encoded as hex (qt-project.org/doc/qt-4.8/qt.html#DropAction-enum). Why are some enumerated integers represented in hex notation in the docs, others in standard decimal notation? Does it matter? My hunch is it shouldn't matter as they are all compared in binary when you do the bitwise or/and. But if that is the case, why would the documentation not just use a single convention? – eric Oct 12 '14 at 17:22
  • 1
    I don't know for sure, but I would guess that the hex ones are intended to be OR'd together, whilst the others are just opaque enumerated values. – ekhumoro Oct 12 '14 at 21:57
  • @ekhumoro it can't be that, unfortunately. I have been using itemFlag as the basis for my understanding of how Qt handles bitmasking, and itemFlag uses integers (qt-project.org/doc/qt-4.8/qt.html#ItemFlag-enum) and supports the same OR'ing ("The ItemFlags type is a typedef for QFlags. It stores an OR combination of ItemFlag values."). Inexplicable difference? Indeed, if you go through the Qt namespace (http://qt-project.org/doc/qt-4.8/qt.html#details) it seems pretty much random whether the flag is described in decimel versus hex. – eric Oct 13 '14 at 01:58
  • Asked about this at qtforum too: http://www.qtcentre.org/threads/60515-Qt-namespace-flags-hexadecimal-versus-decimal . – eric Oct 13 '14 at 13:43
  • 1
    Yes, it looks like there's no real consistency in how the values are formatted in the docs. However, there does seem to be consistency in explicitly stating whether the values are intended to OR'd together. The specific values are strictly irrelevant, though: only the constant *names* really matter. The values of a C++ enumeration will always be some kind of integral type, so it will always be *possible* to perform bitwise operations on them. But there's no way to tell from the enumeration type or it's actual values whether they can be *usefully* OR'd together - you just have to read the docs. – ekhumoro Oct 13 '14 at 16:00
  • 1
    PS: in PyQt, enumerations are subclasses of python's int type. – ekhumoro Oct 13 '14 at 16:13

1 Answers1

2

Background

First, make sure you understand how the bitwise-encoding works for these flags. There are really good descriptions in the accepted answers here:

Everyone that uses Qt and its relatives should read them, they will save a lot of headache if you ever want to extract information from the bitwise-encoded values.

Solution

While the following isn't for drop actions, but for item data roles, the principles are the exact same. As mentioned in the comments on the original post, you can recast the encoded value as an int and then decode it to a human-readable format using the enumeration (i.e., the translation between integer and role) provided by Qt online. I don't know why sometimes the docs represent the integers as hex versus decimals.

In what follows, I represented the enumeration that I found online in a dictionary with the int as key, and the human-readable string description as value. Then use a function that casts the role as an int to do the translation, using that dictionary.

#Create a dictionary of all data roles
dataRoles = {0: 'DisplayRole', 1: 'DecorationRole', 2: 'EditRole', 3: 'ToolTipRole',\
            4: 'StatusTipRole', 5: 'WhatsThisRole', 6: 'FontRole', 7: 'TextAlignmentRole',\
            8: 'BackgroundRole', 9: 'ForegroundRole', 10: 'CheckStateRole', 13: 'SizeHintRole',\
            14: 'InitialSortOrderRole', 32: 'UserRole'} 

#Return role in a human-readable format
def roleToString(flagDict, role):
    recastRole = int(role)  #recast role as int
    roleDescription = flagDict[recastRole]
    return roleDescription

Then to use it, for instance in a model where roles are being thrown around and I want to see what's doing:

print "Current data role: ", roleToString(dataRoles, role)

There are different ways to do it, but I find this very intuitive and easy to use.

sunyata
  • 1,843
  • 5
  • 27
  • 41
eric
  • 7,142
  • 12
  • 72
  • 138