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
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"