如何将qInfo()转换为QString?

huangapple go评论72阅读模式
英文:

How to get qInfo() as a QString?

问题

我试图从C++中访问QML中MapQuickItem的坐标。纬度有9位小数位,经度有8位小数位。但是,如果我尝试使用latitude()来访问它们,我会失去最后的5位小数位。如果我尝试使用toString(QGeoCoordinate::Degrees),我只会得到5位小数位。但当我直接使用qInfo()打印QGeoCoordinate时,它显示了所有9位小数位。

有没有办法获取qInfo()的输出并将其存储在一个QString中,或以其他方式获取纬度和经度的所有小数位?

C++代码部分:

QObject *pin = qvariant_cast<QObject *>(mapObject->property("destination"));
QGeoCoordinate dest = qvariant_cast<QGeoCoordinate>(pin->property("coordinate"));
qInfo() << "dest: " << dest;
qInfo() << dest.toString(QGeoCoordinate::Degrees);
qInfo() << dest.latitude();
qInfo() << dest.longitude();

输出部分:

dest: QGeoCoordinate(33.751513868, -118.1781581, 0)
"33.75151°, -118.17816°, 0m"
33.7515
-118.178

这些是你要翻译的部分。

英文:

I'm trying to access the coordinates of a MapQuickItem in QML from C++. The latitude has 9 decimal digits and longitude has 8 decimal digits. But if I try to access them using latitude(), I lose the last 5 decimal places. If I try to use toString(QGeoCoordinate::Degrees), I only get 5 decimal digits. But when I print out the QGeoCoordinate directly using qInfo(), it shows all 9 digits.

Is there a way to grab the qInfo() output and store them inside a QString, or otherwise get the full decimal places of the latitude and longitude?

C++:

QObject *pin = qvariant_cast&lt;QObject *&gt;(mapObject -&gt; property(&quot;destination&quot;));
QGeoCoordinate dest = qvariant_cast&lt;QGeoCoordinate&gt;(pin-&gt;property(&quot;coordinate&quot;));
qInfo() &lt;&lt; &quot;dest: &quot; &lt;&lt; dest;
qInfo() &lt;&lt; dest.toString(QGeoCoordinate::Degrees);
qInfo() &lt;&lt; dest.latitude();
qInfo() &lt;&lt; dest.longitude();

Output:

dest:  QGeoCoordinate(33.751513868, -118.1781581, 0)
&quot;33.75151&#176;, -118.17816&#176;, 0m&quot;
33.7515
-118.178

答案1

得分: 1

你可以使用qSetRealNumberPrecisionQString::number

double d = 123456789.987654321;
qInfo() &lt;&lt; qSetRealNumberPrecision(15) &lt;&lt; d;
qInfo() &lt;&lt; QString::number(d, 'f', 6);

输出

123456789.987654
"123456789.987654"
QGeoCoordinate coordinate(33.751513868, -118.1781581, 0);

qInfo() &lt;&lt; coordinate;
qInfo() &lt;&lt; qSetRealNumberPrecision(11) &lt;&lt; coordinate.latitude()
                                    &lt;&lt; coordinate.longitude();
qInfo() &lt;&lt; QString::number(coordinate.latitude(), 'f', 9)
        &lt;&lt; QString::number(coordinate.longitude(), 'f', 9);

输出

QGeoCoordinate(33.751513868, -118.1781581, 0)
33.751513868 -118.1781581
"33.751513868" "-118.178158100"
英文:

You can either use qSetRealNumberPrecision or QString::number.

double d = 123456789.987654321;
qInfo() &lt;&lt; qSetRealNumberPrecision(15) &lt;&lt; d;
qInfo() &lt;&lt; QString::number(d, &#39;f&#39;, 6);

Output

123456789.987654
&quot;123456789.987654&quot;

QGeoCoordinate coordinate(33.751513868, -118.1781581, 0);

qInfo() &lt;&lt; coordinate;
qInfo() &lt;&lt; qSetRealNumberPrecision(11) &lt;&lt; coordinate.latitude()
                                       &lt;&lt; coordinate.longitude();
qInfo() &lt;&lt; QString::number(coordinate.latitude(), &#39;f&#39;, 9)
        &lt;&lt; QString::number(coordinate.longitude(), &#39;f&#39;, 9);

Output

QGeoCoordinate(33.751513868, -118.1781581, 0)
33.751513868 -118.1781581
&quot;33.751513868&quot; &quot;-118.178158100&quot;

huangapple
  • 本文由 发表于 2023年4月4日 14:46:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75926253.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定