Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make unicode from variable containing QString

Tags:

python

qt

qt4

pyqt

I have QPlainTextEdit field with 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?

like image 812
rapid Avatar asked Nov 25 '10 21:11

rapid


1 Answers

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

like image 145
serge_gubenko Avatar answered Sep 18 '22 03:09

serge_gubenko