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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…