PDA

View Full Version : Putting unicode in QString literals



reddish
19th November 2009, 08:41
Hi all,

Is there a convenient way to put Unicode characters in a QString literal?

The best I've come up with so far is something like this:

const QChar euroSymbol(0x20ac);
const QString somestring = QString("The Euro symbol is: '") + euroSymbol + Qstring("'.");

The problem with this is that it is rather unclear, and that it cannot nicely be subjected to tr() for internationalization.

This is the one case I yearn for Java (where you can put \u escape codes in a string) although I realize Qt is not to blame for C++ shortcomings in this area.

Regards, Sidney

Lykurg
19th November 2009, 09:07
you can encode your files in utf8 and then you can directly write €! therefore you must also use trUtf8(). Instead of using "+" to concat the string use e.g. tr("dsf %1 ...").arg(...) or QString("...").arg()

miwarre
19th November 2009, 10:09
This is the one case I yearn for Java (where you can put \u escape codes in a string) although I realize Qt is not to blame for C++ shortcomings in this area.
Well, it is not a C++ shortcoming. C++ allows the following declaration:

const wchar_t somestring = L"The Euro symbol is: \x20ac.";
A bit cumbersome is that, to have a Qt QString from it, you have to use the QString::fromWCharArray() function:

[...]->setText(QString::fromWCharArray(somestring) );
which of course cannot be put in the declaration:

const QString somestring = QString::fromWCharArray(L"The Euro symbol is: \x20ac."); // not legal!
A QString constructor accepting a wchar_t[] as parameter could be useful...

M.

P.S.: if the characters following the \x escape sequence could be interpreted as hex digits, the wchar_t string must be split:

const wchar_t somestring = L"The Euro symbol is: \x20ac" "ABCD";

hongquan
30th October 2012, 01:25
you can encode your files in utf8 and then you can directly write €! therefore you must also use trUtf8(). Instead of using "+" to concat the string use e.g. tr("dsf %1 ...").arg(...) or QString("...").arg()

Thank you.