Labels

17/02/2011

Qt display ° (temperature degree) character in Linux

In my Qt program, I would like to display ° character for temperature.
For example:
 
QLabel *tempLabel = new QLabel("37°C");


Although the ° character will ouput correctly on Windows, it gives strange things instead on Linux.

This is what I found from Qt forum:
"QString by default assumes a conversion from C Strings using a local 8 bit encoding which is different for different systems. Therefore what works on Windows will not work on Linux. You need to explicitely set what encoding should be used either by QTextCodec::setCodecForCStrings() or by explicitely saying which codec to use for a particular conversion, for instance QString::fromUtf8()."

Here is my new code:
QLabel *tempLabel = new QLabel("37" + QString::fromUtf8("°") + "C");

No comments:

Post a Comment