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. Selenium IDE编辑区域修改操作学习

    1.修改command.target.value,选择需要修改的步骤,然后点击下方,既可以直接进行修改. 2.添加新的操作步骤:直接在下方编辑区域的下方点击,然后输入或者选择操作类型,然后点击Targ ...

  2. (android实战)破解apk

    简单的总结几个关键步骤: 一.工具准备:apktool , dex2jar , jd-gui 二.使用dex2jar + jd-gui 得到apk的java源码 1.用解压工具从 apk包中取出 cl ...

  3. 4.9 Routing -- Query Parameters

    一.概述 1. 在URL中查询参数是可选的key-value对,出现在?的右边.例如,下面的URL有两个查询参数,sort和page,对应的值分别是ASC和2. example:http://exam ...

  4. 1.初步认识JVM -- JVM序列

    1.JVM概念 JVM是java Virtual Machine的简称.也称为Java虚拟机. 虚拟机:通过软件模拟具有完整硬件功能的运行在一个完全隔离环境的完整计算机系统.VMWare.Visual ...

  5. 39XML文档类

    Xml源代码 domxml.h #ifndef DOMXML_H #define DOMXML_H #include <QString> #include <QStringList& ...

  6. uva1025 dp

    这题说的是给了n个车站 从1号 车站到 n号车站,有m1辆车从1 开往n 有m2 辆车从n 开往1 一个人从1 车站 到达n 车站在T 时刻 要求再 车站呆的时间尽量少 dp[i][j] 表示 在 第 ...

  7. python面向对象编程基础

    演示了 Python 类与对象的编程基础, 包括属性.方法.继承.组合.动态创建类. python 版本: 2.7.5 class SimpleClass(object): ''' a simple ...

  8. DB杂记

    1. mybatits 批量插入: <insert id="insertColumnitem2"> INSERT INTO REPORT_COLUMNITEM (COL ...

  9. 20145219《网络对抗》MSF基础应用

    20145219<网络对抗>MSF基础应用 基础问题回答 用自己的话解释什么是exploit,payload,encode exploit:把实现设置好的东西送到要攻击的主机里. payl ...

  10. ZLYD团队第一周项目总结

    ZLYD团队第一周项目总结 团队项目 项目内容:我们打算利用Applet实现一个吃豆子游戏,团队初步设定游戏规则如下: 按空格键,游戏开始: 通过方向键控制吃豆者的运动方向,直到吃光所有金豆子: 吃到 ...