如何从QVariant获取原始python数据 [英] How to get the original python data from QVariant

查看:216
本文介绍了如何从QVariant获取原始python数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这些天我只是在学习python和Qt.因此,请考虑这将是一个新手问题,但是我被困在这里.

I am just learning python and Qt these days. So please consider that this will be a newbie question, but I am stuck here.

import sys
from PyQt4.QtCore import *

data1 = 'string'
data2 = QVariant(data1)
data3 = data2.toPyObject()

我希望data3与data1'string'相同.但是在我的系统中,data3是

I expected data3 is the same as data1, 'string'. However in my system data3 is

PyQt4.QtCore.QString(u'string')

如果我想处理的数据像示例一样简单,那没什么大不了的,但是我想处理"dict"类型的数据,所以我需要解决此问题.

It is not a big deal if the data I want to handle is simple like example, but I want to handle 'dict' type data so I need to fix this problem.

我认为这是编码问题,但找不到解决方法.

I think this is encoding problem, but can't find how to fix it.

*在每个文档中我都声明:

*In every document I am declaring that:

#-*- coding: utf-8 -*-

推荐答案

您可以通过将数据包装在不可变容器中来解决此问题:

You can work around this issue by wrapping your data in an immutable container:

>>> from PyQt4.QtCore import QVariant
>>> data = {'key1': 123, 'key2': 456}
>>> v = QVariant((data,))
>>> v.toPyObject()[0]
{'key2': 456, 'key1': 123}

这篇关于如何从QVariant获取原始python数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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