PDA

View Full Version : Converting QGeoCoordinate to a string and the splitting values to another list



Corny
13th May 2019, 19:07
I have a QGeoCoordinate named newPosDeg. While debugging, the Locals and Expressions window shows the following value(s) for the variable

Locals and Expressions

this

newPosDeg @0x19b9918c QGeoCoordinate

d @0x19b9918c QSharedDataPointer<QGeoCoordinatePrivate>

[QSharedData] ref: 1 QSharedData
alt 90.0 double
lat 35.2371746642 double
lng -118.002151755 double


These are the values expected. However, when I attempt to split this QGeoCoordinate using the QGeoCoordinate::CoordinateFormat of DegreesMinutesSecondsWithHemisphere to a StringList called posList with the command...


QStringList posList = newPosDeg.toString(degFormat).split(',');
the value of posList in the Locals and Expressions window is as follows...


posList

[0] "35° 14' 13.8" N"
[1] " 118° 0' 7.7" W"
[2] " 90m"

Note the space prepended in front of the value for elements 1 and 2, and not element 0.
Does anyone know if this is the standard behavior for the split function used in this manner?

Ginsengelf
14th May 2019, 07:36
Hi, that space has always been there because QGeoCoordinate::toString generates a string like this: "27° 28' 3.2" S, 153° 1' 40.4" E, 28.1m", and splitting does not remove anything except the separator character/string.
You could split with ", " (string with comma followed by space) instead of ','.

Ginsengelf

d_stranz
16th May 2019, 19:27
You could split with ", " (string with comma followed by space) instead of ','.

In which case I think you'd need to turn off the KeepEmptyParts option to split(). The combination of "," and " " consecutively would generate an empty string in the list, I think.

Corny
16th May 2019, 19:52
Thanks d_stranz for helping. You stated...


In which case I think you'd need to turn off the KeepEmptyParts option to split(). The combination of "," and " " consecutively would generate an empty string in the list

When I first tried it, I was thinking the same thing, however it turns out that I didn't need to change the behavior. The documentation states...

If behavior is QString::SkipEmptyParts, empty entries don't appear in the result. By default, empty entries are kept.

so it worked just great with the default behavior.

Thanks again. :D

d_stranz
16th May 2019, 21:01
I'm not in front of a PC with Qt on it, so I wasn't sure whether Keep or Skip was the default. It's something to keep in mind though when using split().