英文:
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<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();
Output:
dest: QGeoCoordinate(33.751513868, -118.1781581, 0)
"33.75151°, -118.17816°, 0m"
33.7515
-118.178
答案1
得分: 1
你可以使用qSetRealNumberPrecision或QString::number。
double d = 123456789.987654321;
qInfo() << qSetRealNumberPrecision(15) << d;
qInfo() << QString::number(d, 'f', 6);
输出
123456789.987654
"123456789.987654"
QGeoCoordinate coordinate(33.751513868, -118.1781581, 0);
qInfo() << coordinate;
qInfo() << qSetRealNumberPrecision(11) << coordinate.latitude()
<< coordinate.longitude();
qInfo() << QString::number(coordinate.latitude(), 'f', 9)
<< 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() << qSetRealNumberPrecision(15) << d;
qInfo() << QString::number(d, 'f', 6);
Output
123456789.987654
"123456789.987654"
QGeoCoordinate coordinate(33.751513868, -118.1781581, 0);
qInfo() << coordinate;
qInfo() << qSetRealNumberPrecision(11) << coordinate.latitude()
<< coordinate.longitude();
qInfo() << QString::number(coordinate.latitude(), 'f', 9)
<< QString::number(coordinate.longitude(), 'f', 9);
Output
QGeoCoordinate(33.751513868, -118.1781581, 0)
33.751513868 -118.1781581
"33.751513868" "-118.178158100"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论