How to convert a QString to unicode object in python 2?

I had this problem to solve, and I tried to find the safest way. This program illustrates the solution

from PyQt4 import QtCore, QtGui
import sys app = QtGui.QApplication(sys.argv)
ed = QtGui.QLineEdit() def editingFinished():
# The text() returns a QString, which is unicode-aware
print type(ed.text())
# This returns a QByteArray, which is the encoded unicode string in the utf-8 encoding.
print type(ed.text().toUtf8())
# Since unicode() accepts a sequence of bytes, the safest and fully controlled way of performing
# the transformation is to pass the encoded sequence of bytes, and let unicode know it is utf-8 encoded
print unicode(ed.text().toUtf8(), encoding="UTF-8") QtCore.QObject.connect(ed, QtCore.SIGNAL("editingFinished()"), editingFinished)
ed.show() app.exec_()

So the solution is

unicode(qstring_object.toUtf8(), encoding="UTF-8")

Maybe there’s another, simpler way that it’s also safe, but for now this solution is good enough.

How to convert a QString to unicode object in python 2?的更多相关文章

  1. appium 提示报错“TypeError: 'unicode' object is not callable”的解决方式!

    这里提到的这个报错,是小错误且容易经常会犯,有时需要特别注意使用. 目的要求结果:根据某个元素的id值获取到对应id的text值,并且将获取的text值与本身存在的text值做比较,查看text值是否 ...

  2. 【Python】unicode' object is not callable

    在Python中,出现'unicode' object is not callable的错误一般是把字符串当做函数使用了.

  3. celery:Unrecoverable error: AttributeError("'unicode' object has no attribute 'iteritems')

    环境描述 python2+django1.9下使用celery异步处理耗时请求. celery使用的是celery-with-redis这个第三方库,版本号为3.0. pip install cele ...

  4. [Python] 'unicode' object is not callable

    在Python中,出现'unicode' object is not callable的错误一般是把字符串当做函数使用了.

  5. AttributeError: 'unicode' object has no attribute 'tzinfo' 未解决

    Internal Server Error: /demo/machineinfo.htmlTraceback (most recent call last): File "C:\Python ...

  6. Jpa自定义查询报错(Failed to convert from type [java.lang.Object[]] to type)

    Jpa自定义查询报错 问题背景 今天遇到一个奇怪的报错"Failed to convert from type [java.lang.Object[]] to type",这个报错 ...

  7. convert URL Query String to Object All In One

    convert URL Query String to Object All In One URL / query string / paramas query string to object le ...

  8. 《Using Databases with Python》Week1 Object Oriented Python 课堂笔记

    Coursera课程<Using Databases with Python> 密歇根大学 Charles Severance Week1 Object Oriented Python U ...

  9. paip.utf-8,unicode编码的本质输出unicode文件原理 python

    paip.utf-8,unicode编码的本质输出unicode文件原理 python      #别的语言,java php都是unicode,走十python不一样.    #enddef  #t ...

随机推荐

  1. Spark SQL的整体实现逻辑

    1.sql语句的模块解析 当我们写一个查询语句时,一般包含三个部分,select部分,from数据源部分,where限制条件部分,这三部分的内容在sql中有专门的名称: 当我们写sql时,如上图所示, ...

  2. 一、怎样使用eclipse查看JDK源码

    前言: JDK是整个java开发的核心,它包含了JAVA的运行环境,JAVA工具和JAVA基础的类库.阅读一些系统的源码会帮助你理解一些基本的原理. 一.创建一个工程 在eclipse中创建一个jav ...

  3. centos配置用户级别的jdk的环境变量

    前面讲解了centos配置jdk的环境变量 的root级别的jdk配置 ,这里讲解用户级别的jdk配置. 在用户的当前目录下,如下,有四个隐藏的文件,文件打头是.bash******: 1.编辑.ba ...

  4. 通过J2EE Web工程添加Flex项目,进行BlazeDS开发

    http://www.cnblogs.com/noam/archive/2010/07/22/1782955.html 环境:Eclipse 7.5 + Flex Builder 4 plugin f ...

  5. supervisor初试

    Supervisor (http://supervisord.org) 是一个用 Python 写的进程管理工具,可以很方便的用来启动.重启.关闭进程(不仅仅是 Python 进程).除了对单个进程的 ...

  6. 20145303刘俊谦《网络对抗》Exp2 后门原理与实践

    20145303刘俊谦<网络对抗>Exp2 后门原理与实践 基础问题回答 1.例举你能想到的一个后门进入到你系统中的可能方式? •在网页上浏览不安全的网站或者下载不安全的软件 •通过发送邮 ...

  7. Asynchronous Programming Patterns

    Asynchronous Programming Patterns The .NET Framework provides three patterns for performing asynchro ...

  8. 【Rest】在Dubbo中开发REST风格的远程调用(RESTful Remoting)

    目录 概述 REST的优点 应用场景 快速入门 标准Java REST API:JAX-RS简介 REST服务提供端详解 HTTP POST/GET的实现 Annotation放在接口类还是实现类 J ...

  9. 科幻小说《霜与火》 by 雷·布雷德伯里

    漫漫长夜中,西姆出世了,躺在山洞冰凉的石头上,嗷嗷大哭,浑身血液奔流,脉搏每分钟一千跳.他不停地长大.妈妈用发烫的双手喂西姆吃东西.人生的梦魔开场了.一来到世间,他的眼睛就闪烁着警觉的光芒:而后又令人 ...

  10. LA 2963 超级传输(扫描)

    https://vjudge.net/problem/UVALive-2963 题意:需要在n个星球上各装一个广播装置,作用范围均为R.每个星球广播A类节目或者B类节目.a表示星球i收听到的和自己广播 ...