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

孤街醉人 提交于 2019-12-09 18:51:38

问题


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.


回答1:


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.




回答2:


Try

return QString::fromLatin1(text);

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




回答3:


QT5 changed Qt's default codec from Latin-1 to UTF-8, as described here: https://www.macieira.org/blog/2012/05/source-code-must-be-utf-8-and-qstring-wants-it/

Latin-1 and Unicode both use 176 (0xB0 or 0260) as the degree symbol, so your usage of it coincidentally worked, since it was interpreted as Latin-1 and converted to the same value in Unicode.

That first line could be changed to:

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

As mentioned before, going directly to a QString is indeed better, but if you had to go through a std::string, you could simply substitute the UTF-8 encoding of Unicode 176, in which the lower 6 bits 110000 would have a 10 prepended, and the upper 2 bits 10, would have 110000 prepended in the first byte. This becomes: \302\260.



来源:https://stackoverflow.com/questions/18758252/how-to-print-degree-symbol-on-the-window-using-qt5qtquick-2-1-and-above

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!