开发者

Make unicode from variable containing QString

开发者 https://www.devze.com 2023-01-27 13:09 出处:网络
I have QPlainTextEdit field with 开发者_JS百科data containing national characters (iso-8859-2). tmp = self.ui.field.toPlainText() (QString type)

I have QPlainTextEdit field with 开发者_JS百科data containing national characters (iso-8859-2).

tmp = self.ui.field.toPlainText() (QString type)

When I do:

tmp = unicode(tmp, 'iso-8859-2') 

I get question marks instead of national characters. How can I convert properly the data in QPlainTextEdit field to unicode?


As it was said QPlainTextEdit.toPlainText() returns QString which should be UTF-16, whereas unicode() constructor expects a byte string. Below is a small example:

tmp = self.field.toPlainText()
print 'field.toPlainText: ', tmp

codec0 = QtCore.QTextCodec.codecForName("UTF-16");
codec1 = QtCore.QTextCodec.codecForName("ISO 8859-2");  

print 'UTF-16: ', unicode(codec0.fromUnicode(tmp), 'UTF-16')
print 'ISO 8859-2: ', unicode(codec1.fromUnicode(tmp), 'ISO 8859-2')

this code produces following output:

field.toPlainText: test ÖÄ это китайский: 最主要的

UTF-16: test ÖÄ это китайский: 最主要的

ISO 8859-2: test ÖÄ ??? ?????????: ????

hope this helps, regards

0

精彩评论

暂无评论...
验证码 换一张
取 消