pyqt4 处理中文问题?有人碰到过吗?

pyqt4 处理中文问题?有人碰到过吗?



[Copy to clipboard] [ - ]
CODE:
>>> import sys
>>> from PyQt4 import QtCore, QtGui
>>> app=QtGui.QApplication(sys.argv)
>>> win = QtGui.QLineEdit()
>>> win.setText((QtGui.QApplication.translate("gongzi", "你好", None, QtGui.QApplication.UnicodeUTF8)))
>>> a = QtCore.QString(win.text())
>>> print a
Traceback (most recent call last):
  File "<input>", line 1, in ?
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-7: ordinal not in range(128)

>>> win.setText((QtGui.QApplication.translate("gongzi", "111", None, QtGui.QApplication.UnicodeUTF8)))
>>> a = QtCore.QString(win.text())
>>> print a
111

你应该使用unicode吧。因为你是在交互环境下做的,因此可以:

a = unicode('你好', 'gbk')
然后把a传进去试试。


[Copy to clipboard] [ - ]
CODE:
>>> win.setText((QtGui.QApplication.translate("gongzi", "你好", None, QtGui.QApplication.UnicodeUTF8)))
>>> a = unicode(QtCore.QString(win.text()),'gbk')
>>> type (a)
<type 'unicode'>
>>> b = a.encode("gbk")
>>> print b
????????
>>>a = unicode("你好",'gbk')
>>>print a
你好

打印得时候还是有问题,比较郁闷还是怀疑QtCore.QString  的问题!
win.setText((QtGui.QApplication.translate("gongzi", "你好", None, QtGui.QApplication.UnicodeUTF))

这是用的UTF8编码吗?可是你的中文串并不是utf-8的啊,它与你的交互环境用的编码一致。