PDA

View Full Version : Encoding problem with QTextEdit



alexandernst
2nd September 2009, 11:17
I'm trying to append a string ("æææ" for example) to a QTextEdit object with .append().
If I do this:

print mystring
mywidget.append("mystring")

The output in console is "æææ", but the chars that I get in the UI ar A¡A¡A¡. What's the problem?

The default encoding in my app is utf-8.

victor.fernandez
2nd September 2009, 11:35
What data type is mystring? Is it a QString? Did you try using QString::fromUtf8() or setting the proper codec? If you use the QString::QString(const char *) constructor (e.g. QString mystring = "æææ";), it uses fromAscii(), which is the same as fromLatin1() unless you set a default codec. As the Qt docs state:


QString::QString ( const char * str )

Constructs a string initialized with the ASCII string str. The given const char pointer is converted to Unicode using the fromAscii() function.


String QString::fromAscii ( const char * str, int size = -1 ) [static]

Returns a QString initialized with the first size characters of the 8-bit ASCII string str.

If size is -1 (the default), it is taken to be qstrlen(str).

If a codec has been set using QTextCodec::setCodecForCStrings(), it is used to convert str to Unicode; otherwise this function does the same as fromLatin1().

alexandernst
2nd September 2009, 11:40
No, it's a python utf8 string.

alexandernst
2nd September 2009, 11:49
I fixed it :)
mystring.decode("utf-8")