How to print degree symbol on the window using qt5(QtQuick 2.1) and above
Solution 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.
Solution 2
Try
return QString::fromLatin1(text);
or, if that doesn't work, another static QString::fromXXX method.
Solution 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
.
Related videos on Youtube
zeal
Updated on June 06, 2022Comments
-
zeal about 2 years
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 usinfUTF
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.
-
zeal almost 11 yearsplease see the revised problem and take some pain to provide solution. Thanks for the last comment.
-
zeal almost 11 yearsHey itried all option but the error i am getting is ths "none of the 2 overloads could convert all the argument types". i don't know why but i must tell you i am also printing character 'N' and 'E' and it depending on position of cursor on globe to 'S' or 'W'. I write these in question because i thought it would not matter i don't it matters of not. Please give your thoughts.
-
zeal almost 11 yearsThanks Marek answer provided by you seems to be working. With change that in fourth argument of function should be arg(latD, 2, 10, QLatin1Char('0')); if 4th argument is required.