PDA

View Full Version : Any way to put Unicode character in QString without using QString("%1").arg(QChar)?



oddity
12th November 2013, 22:58
Is it possible to specify QStrings which are texts containing extended ASCII accented characters, coded as Unicode, and have these correctly rendered when output using qDebug()? Eg., create a QString with the text "Texte en français" as QString("Texte en fran\u00C7ais") and have it show up, say when printed with qDebug(), as "Texte en français".

I know that if I get to the QString using QString::fromLocal8Bit("Texte en français"), it will work--the output text is "Texte en français", and I can use this approach if necessary. I'm just wondering why, if I do QString("Texte en fran\u00C7ais"), it doesn't work--I get an output of "Texte en franÇais" with the à followed by a small square containing what appears to be an 00 87.

Note, this isn't an internationalization situation, where I should be using tr(), lupdate, Qt Linguist, lrelease, and so on. I'm just curious as to whether the thing can be done.

Radek
13th November 2013, 07:23
It's a problem of qDebug(), not of QString. The QString handles Unicode correctly. The literals like "Texte en français" will be Unicode "Texte en français" in QString. The "Texte en fran\u00C7ais" will not work in QString, the QString does not know the "\unnnn" escapes and will put "\unnnn" in the QString instead of "nnnn" converted to int.

qDebug() seems to use code pages internally even if the operating system is Unicode (like Linux).

ChrisW67
13th November 2013, 09:03
Your C++ compiler will convert the character literal '\u00c7' to the two UTF8 bytes 0xC3 0x87. That is what will be in the C-string that you construct the QString from.

The Qt4 QString(const char *) constructor will use fromAscii()/fromLatin1() and misinterpret those bytes resulting in two characters in the QString where there should only be one. What you want is this:


QString s(QString::fromUtf8("Texte en fran\u00e7ais"));
// or
QString s = QString::fromUtf8("Texte en fran\u00e7ais");


The Qt5 QString(const char *) constructor will use fromUtf8() so this should be fine:


QString s("Texte en fran\u00e7ais");


On Linux qDebug() will generally do the right thing with strings constructed this way.