Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print degree symbol on the window using qt5(QtQuick 2.1) and above

Tags:

c++

c

utf

qt

qml

When I was using up to qt4.8(qt quick 1.1) for gui then I am successfully able to print degree with \260 but when things got upgraded to qt5 and above then this stopped working. I searched on the net and found many relevant link such as (http://www.fileformat.info/info/unicode/char/00b0/index.htm) I tried but no help. Do I need to include some library for usinf UTF format or problem is sth else. Please some one help. What to do?

@Revised, Here it is described what is being done.

First I am storing the printable statement in string text. As in cpp function:-

                 sprintf(text, "%02d\260  %03d\260 ",latD, longD);

                 QString positionText(text.c_str());
                 return positionText;     

And then using positionText in qml file to display on the window.

So, someone please answer what do I need to do to have degree in display?

Thanks.

like image 776
zeal Avatar asked Dec 16 '22 06:12

zeal


2 Answers

Problem is simple you used \260 most probably inside Ansii C-string (const char []). In such cases Qt has use some codec to convert this to Unicode characters. For some reason when you change Qt version default codec was changed and this is why it stopped working.

Anyway your approach is wrong. You shouldn't use C-string which are codec depended (usually this leads to this kind of problems). You can define QChar const as QChar(0260) or best approach is to use tr and provide translation.

It would be best if you give representative example with string with degree character, then someone will provide you best solution.


Edit:

I would change your code like this:

const QChar degreeChar(0260); // octal value
return QString("%1%3  %2%3").arg(latD, 2, 10, '0').arg(longD, 3, 10, '0').arg(degreeChar);

or add translation which will handle this line:

return tr("%1degree  %2degree").arg(latD, 2, 10, '0').arg(longD, 3, 10, '0');

Note that this translation for this line only have to be added always no mater what is current locale.

like image 92
Marek R Avatar answered Dec 17 '22 18:12

Marek R


Try

return QString::fromLatin1(text);

or, if that doesn't work, another static QString::fromXXX method.

like image 41
Stoeoef Avatar answered Dec 17 '22 19:12

Stoeoef