سؤال

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.

هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

Try

return QString::fromLatin1(text);

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

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.

To easily print angles with degree symbols in console, try this:

#include <QDebug>
double v = 7.0589;
qDebug().noquote() << "value=" << v << QString(248);

Console output:

value= 7.0589 °

This works out-of-the-box under Windows.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top