模型中 dropActions 的访问值 (PySide/PyQt/Qt) [英] Access values of dropActions in model (PySide/PyQt/Qt)

查看:25
本文介绍了模型中 dropActions 的访问值 (PySide/PyQt/Qt)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 QTableModel 中输入以下内容时:

In a QTableModel when I enter the following:

print model.supportedDropActions()

我只知道:

<PyQt4.QtCore.DropActions object at 0x00000000081172E8>

如何从该对象访问支持的放置操作的实际列表?在文档中,它说,The DropActionstype 是 QFlags 的 typedef.它存储 DropAction 值的 OR 组合."

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."

请注意,我是在 Python (PySide) 中执行此操作的.

Note I am doing this in Python (PySide).

相关帖子:

推荐答案

背景

首先,请确保您了解按位编码如何对这些标志起作用.这里接受的答案中有非常好的描述:

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

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

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.

解决方案

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

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.

在下文中,我代表了我在网上找到的枚举 在字典中,以 int 为键,以人类可读的字符串描述为值.然后使用将角色转换为 int 的函数进行翻译,使用该字典.

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.

这篇关于模型中 dropActions 的访问值 (PySide/PyQt/Qt)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆