访问模型中dropActions的值(PySide / PyQt / Qt)

问题描述 投票:3回答:1

在我输入以下内容的QTableModel中:

print model.supportedDropActions()

我得到:

<PyQt4.QtCore.DropActions object at 0x00000000081172E8>

如何从此对象访问受支持的放置操作的实际列表?在the documentation,它说,“DropActions类型是QFlags的typedef。它存储了DropAction值的OR组合。”

注意我在Python(PySide)中这样做。

相关文章:

qt qt4 pyqt pyside
1个回答
2
投票

背景

首先,确保您了解按位编码对这些标志的工作原理。这里接受的答案中有很好的描述:

使用Qt及其亲属的每个人都应该阅读它们,如果您想要从按位编码的值中提取信息,它们将会省去很多麻烦。

虽然以下内容不适用于放置操作,但对于项目数据角色,原则完全相同。正如在原始帖子的评论中所提到的,您可以将编码值重新编码为int,然后使用Qt online提供的枚举(即整数和角色之间的转换)将其解码为人类可读的格式。我不知道为什么有时文档将整数表示为十六进制与十进制。

在下文中,我将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)

有不同的方法,但我觉得这非常直观,易于使用。

© www.soinside.com 2019 - 2024. All rights reserved.