当我输入以下内容时在 QTableModel 中:
print model.supportedDropActions()
我刚刚得到:
<PyQt4.QtCore.DropActions object at 0x00000000081172E8>
如何从该对象访问支持的放置操作的实际列表?在 the documentation ,它说,“DropActions 类型是 QFlags 的类型定义。它存储 DropAction 值的 OR 组合。”
请注意,我是在 Python (PySide) 中执行此操作的。
相关帖子:
最佳答案
背景
首先,请确保您了解这些标志的按位编码是如何工作的。这里接受的答案中有非常好的描述:
- What are bitwise operators?
- How to find specific Qt.ItemFlag occurrence into custom Qt.ItemFlags instance in PyQt?
每个使用 Qt 及其亲属的人都应该阅读它们,如果您想从按位编码的值中提取信息,它们会省去很多麻烦。
解决方案
虽然以下内容不是针对放置操作,但对于项目数据角色而言,原则是完全相同的。如原帖评论中所述,您可以将编码值重新转换为 int
,然后使用提供的枚举(即整数和角色之间的转换)将其解码为人类可读的格式通过 Qt 在线。我不知道为什么有时文档将整数表示为十六进制而不是小数。
接下来,我代表the enumeration that I found online在以 int
为键、以人类可读的字符串描述为值的字典中。然后使用将角色转换为 int
的函数来使用该字典进行翻译。
#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
然后使用它,例如在角色被抛出的模型中,我想看看正在做什么:
print "Current data role: ", roleToString(dataRoles, role)
有多种方法可以做到这一点,但我发现这种方法非常直观且易于使用。
关于qt - 模型中 dropActions 的访问值 (PySide/PyQt/Qt),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26321386/