简体   繁体   English

如何使用 qt5(QtQuick 2.1) 及以上版本在窗口上打印度数符号

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

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.当我将 qt4.8(qt quick 1.1) 用于 gui 时,我可以成功地使用\\260打印学位,但是当事情升级到 qt5 及更高版本时,它就停止工作了。 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.我在网上搜索并找到了许多相关链接,例如( http://www.fileformat.info/info/unicode/char/00b0/index.htm )我尝试过但没有帮助。 Do I need to include some library for usinf UTF format or problem is sth else.我是否需要为 usinf UTF格式包含一些库,或者还有其他问题。 Please some one help.请有人帮忙。 What to do?该怎么办?

@Revised, Here it is described what is being done. @Revised,这里描述了正在做的事情。

First I am storing the printable statement in string text .首先,我将可打印语句存储在字符串text As in cpp function:-在 cpp 函数中:-

                 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.然后使用 qml 文件中的positionText显示在窗口上。

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 [] ). 问题很简单你使用\\260最可能在Ansii C-string( const char [] )里面。 In such cases Qt has use some codec to convert this to Unicode characters. 在这种情况下,Qt使用一些编解码器将其转换为Unicode字符。 For some reason when you change Qt version default codec was changed and this is why it stopped working. 出于某种原因,当您更改Qt版本时,默认编解码器已更改,这就是它停止工作的原因。

Anyway your approach is wrong. 无论如何你的方法是错误的。 You shouldn't use C-string which are codec depended (usually this leads to this kind of problems). 你不应该使用编解码器所依赖的C字符串(通常这会导致这种问题)。 You can define QChar const as QChar(0260) or best approach is to use tr and provide translation. 您可以将QChar const定义为QChar(0260)或者最好的方法是使用tr并提供转换。

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. 或者,如果这不起作用,另一个静态QString :: fromXXX方法。

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/ QT5将Qt的默认编解码器从Latin-1更改为UTF-8,如下所述: https//www.macieira.org/blog/2012/05/source-code-must-be-utf-8-and-qstring-希望-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. Latin-1和Unicode都使用1760xB00260 )作为度符号,因此您对它的使用巧合,因为它被解释为Latin-1并在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. 如前所述,直接转到QString确实更好,但如果你必须通过std::string ,你可以简单地替换Unicode 176的UTF-8编码,其中低6位110000将有10前置,并且高2位10将在第一个字节中加上110000 This becomes: \\302\\260 . 这变为: \\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.这在 Windows 下开箱即用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM