How to convert a QString to unicode object in python 2?
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?的更多相关文章
- appium 提示报错“TypeError: 'unicode' object is not callable”的解决方式!
这里提到的这个报错,是小错误且容易经常会犯,有时需要特别注意使用. 目的要求结果:根据某个元素的id值获取到对应id的text值,并且将获取的text值与本身存在的text值做比较,查看text值是否 ...
- 【Python】unicode' object is not callable
在Python中,出现'unicode' object is not callable的错误一般是把字符串当做函数使用了.
- celery:Unrecoverable error: AttributeError("'unicode' object has no attribute 'iteritems')
环境描述 python2+django1.9下使用celery异步处理耗时请求. celery使用的是celery-with-redis这个第三方库,版本号为3.0. pip install cele ...
- [Python] 'unicode' object is not callable
在Python中,出现'unicode' object is not callable的错误一般是把字符串当做函数使用了.
- AttributeError: 'unicode' object has no attribute 'tzinfo' 未解决
Internal Server Error: /demo/machineinfo.htmlTraceback (most recent call last): File "C:\Python ...
- Jpa自定义查询报错(Failed to convert from type [java.lang.Object[]] to type)
Jpa自定义查询报错 问题背景 今天遇到一个奇怪的报错"Failed to convert from type [java.lang.Object[]] to type",这个报错 ...
- 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 ...
- 《Using Databases with Python》Week1 Object Oriented Python 课堂笔记
Coursera课程<Using Databases with Python> 密歇根大学 Charles Severance Week1 Object Oriented Python U ...
- paip.utf-8,unicode编码的本质输出unicode文件原理 python
paip.utf-8,unicode编码的本质输出unicode文件原理 python #别的语言,java php都是unicode,走十python不一样. #enddef #t ...
随机推荐
- ng-深度学习-课程笔记-9: 机器学习策略1(Week1)
1 为什么要应用机器学习策略( Why is machine learning strategy ) 当你想优化一个问题的时候,通常可以有很多尝试(比如收集更多数据,增加迭代次数,改用adam,改变网 ...
- springcloud7---hystrix
目前使用eureka server完成了服务注册和服务发现,ribbon完成了客户端负载均衡.如果服务提供者的响应很慢那么服务消费者会强制等待,一直等到http请求超时,如果服务消费者还是其他的服务提 ...
- iframe 跨域问题解决方案 利用window.name+iframe跨域获取数据详解
详解 前文提到用jsonp的方式来跨域获取数据,本文为大家介绍下如何利用window.name+iframe跨域获取数据. 首先我们要简单了解下window.name和iframe的相关知识.ifra ...
- 人脸识别技术大总结(1):Face Detection & Alignment
http://blog.jobbole.com/85783/ 首页 最新文章 IT 职场 前端 后端 移动端 数据库 运维 其他技术 - 导航条 - 首页 最新文章 IT 职场 前端 - Ja ...
- 20145314郑凯杰《网络对抗技术》可选实验 shellcode注入与Return-to-libc攻击实验
20145314郑凯杰<网络对抗技术>可选实验 shellcode注入与Return-to-libc攻击实验 1.0 实践内容 Return-to-libc攻击是一种特殊的缓冲区溢出攻击, ...
- 2017-2018-1 JaWorld 团队作业--冲刺4
2017-2018-1 JaWorld 团队作业--冲刺4 (20162305) 总体架构 我们本次团队项目设定为基于Android系统Java架构下的打飞机小游戏 游戏中所有模型的原型设定是精灵,因 ...
- iptable的使用
目录 iptables iptables iptables详解 2012-07-18 20:10:08 分类: LINUX 一:前言 防火墙,其实说白了讲,就是用于实现Linux下访问控制的功能的,它 ...
- zoj 3747 递推dp
Attack on Titans Time Limit: 2 Seconds Memory Limit: 65536 KB Over centuries ago, mankind faced ...
- Recover Binary Search Tree,恢复二叉排序树
问题描述:题意就是二叉树中有两个节点交换了,恢复结构. Two elements of a binary search tree (BST) are swapped by mistake. Recov ...
- java学习之浅谈多线程3--线程间协作
通过保证临界区上多个线程的相互排斥,线程同步完全可以避免竞争状态的发生,但是有时还需要线程之间的协作.有两种方式可用于线程间的通信. 1.使用条件Condition Condition接口: +awa ...