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. 【译】3 ways to define a JavaScript class

    本文真没啥难点,我就是为了检验我英语水平退化了没哈哈虽然我英语本来就渣翻译起来也像大白话.将原文看了一遍也码完翻译了一遍差不多一个小时,其中批注部分是自己的理解如有疏漏或误解还请指出感激不尽呐,比如J ...

  2. google chrome插件开发,自己动手,丰衣足食

    因为平时上网都用chrome,但总感觉除了速度快,简洁以外总还有地方满足不了我的需要,然后找插件…后来发现,插件虽然海量但找个称心如意的也不是件容易的事儿,用在找插件的时间都能自己写一个了,于是,今年 ...

  3. leetcode 数据库题解

    184. Department Highest Salary 题意: The Employee table holds all employees. Every employee has an Id, ...

  4. hashset和treeset的区别

    hashset 和 treeset的区别 hashset 和 treeset的区别 1.TreeSet 是二差树实现的,Treeset中的数据是自动排好序的,不允许放入null值. 2.HashSet ...

  5. ZW云推客即将登场

    ZW云推客即将登场   即"4K云字库"框架文件发布后,ZW团队即将发布一全功能的:ZW云推客系统. ZW云推客系统,是原zBrow"百万社区营销系统".&qu ...

  6. spring AOP的注解实例

    上一篇写了spring AOP 的两种代理,这里开始AOP的实现了,个人喜欢用注解方式,原因是相对于XML方式注解方式更灵活,更强大,更可扩展.所以XML方式的AOP实现就被我抛弃了. 实现Sprin ...

  7. 20145319 return-to-libc攻击实验

    20145319 Return-to-libc攻击实验 一 实验内容 return-to-libc实验是一个基于缓冲区溢出攻击实验的基础上的一种攻击实验 缓冲区溢出攻击相关知识: 原理:通过一段包含s ...

  8. 什么是OPTEE-OS

    1. 为什么会出现这种技术? 为了安全,例如:保护指纹虹膜的生物特征数据 2. 为了确保数据安全各家公司都做了些什么? Arm公司提出的了trustzone技术,用一根安全总线(称为NS位)来判断当前 ...

  9. Solidity 官方文档中文版 3_安装Solidity

    基于浏览器的Solidity 如果你只是想尝试一个使用Solidity的小合约,你不需要安装任何东西,只要访问 基于浏览器的Solidity http://remix.ethereum.org/. 如 ...

  10. BZOJ 2806 【CTSC2012】 Cheat

    题目链接:Cheat 话说这道题很久以前某人就给我们考过,直到现在,我终于把这个坑填上了…… 这道题要我们把一个串\(S\)划分成若干块,每块长度不小于\(L_0\),使得能够在文章库中完全匹配的块的 ...