Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Split QString generated from QGeoCoordinate.
Qt 6.11 is out! See what's new in the release blog

Split QString generated from QGeoCoordinate.

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 4 Posters 995 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Christian EhrlicherC Offline
    Christian EhrlicherC Offline
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on last edited by
    #2

    Simply don't use a regex...

    QVector<QStringRef> parts = s.splitRef(QLatin1String(", "));
    if (parts.size() < 2 || parts.size() > 3)
      return QGeoCoordinate();
    if (parts[0].endsWith('°')
      parts[0] = parts[0].chop(1);
    if (parts[1].endsWith('°')
      parts[1] = parts[1].chop(1);
    if (parts.size() == 3 && parts[2].endsWith('m'))
      parts[2] = parts[2].chop(1);
    return QGeoCoordinate(parts.at(0).toDouble(), parts.at(1).toDouble(), parts.size() == 2 ? 0.0 : parts.at(2).toDouble() );
    

    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
    Visit the Qt Academy at https://academy.qt.io/catalog

    G 1 Reply Last reply
    0
    • Christian EhrlicherC Christian Ehrlicher

      Simply don't use a regex...

      QVector<QStringRef> parts = s.splitRef(QLatin1String(", "));
      if (parts.size() < 2 || parts.size() > 3)
        return QGeoCoordinate();
      if (parts[0].endsWith('°')
        parts[0] = parts[0].chop(1);
      if (parts[1].endsWith('°')
        parts[1] = parts[1].chop(1);
      if (parts.size() == 3 && parts[2].endsWith('m'))
        parts[2] = parts[2].chop(1);
      return QGeoCoordinate(parts.at(0).toDouble(), parts.at(1).toDouble(), parts.size() == 2 ? 0.0 : parts.at(2).toDouble() );
      
      G Offline
      G Offline
      Gourmet
      wrote on last edited by Gourmet
      #3

      @Christian-Ehrlicher in this case simpler use QStringList and QRegularExpression(", "). The code

      QStringList list = s.split( QRegularExpression(", "), QString::SkipEmptyParts );
      

      gives list == {"-27.46758°", "153.02789°", "28.1m"} or {"-27.46758°", "153.02789°"}. No endsWith() checking needed - if line exists it always ends with '°' or 'm'. But I wanted perform all in one split operation.

      1 Reply Last reply
      0
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #4

        @Gourmet said in Split QString generated from QGeoCoordinate.:

        But I wanted perform all in one split operation.

        Why? To make it slower? To be someone who wrote a regexp?

        Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        G 1 Reply Last reply
        0
        • Christian EhrlicherC Christian Ehrlicher

          @Gourmet said in Split QString generated from QGeoCoordinate.:

          But I wanted perform all in one split operation.

          Why? To make it slower? To be someone who wrote a regexp?

          Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.

          G Offline
          G Offline
          Gourmet
          wrote on last edited by
          #5

          @Christian-Ehrlicher to make code shorter...

          JKSHJ 1 Reply Last reply
          0
          • G Gourmet

            @Christian-Ehrlicher to make code shorter...

            JKSHJ Offline
            JKSHJ Offline
            JKSH
            Moderators
            wrote on last edited by
            #6

            @Gourmet said in Split QString generated from QGeoCoordinate.:

            to make code shorter...

            Readable code is more important than short code.

            https://softwareengineering.stackexchange.com/questions/203684/is-fewer-lines-of-code-always-better

            Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

            G 1 Reply Last reply
            1
            • JKSHJ JKSH

              @Gourmet said in Split QString generated from QGeoCoordinate.:

              to make code shorter...

              Readable code is more important than short code.

              https://softwareengineering.stackexchange.com/questions/203684/is-fewer-lines-of-code-always-better

              G Offline
              G Offline
              Gourmet
              wrote on last edited by
              #7

              @JKSH less to read means easer to read.

              1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #8

                @Gourmet said in Split QString generated from QGeoCoordinate.:

                less to read means easer to read.

                So you say me you can read a regexp (which you are not aware to create on your own so I guess you won't understand it later on) is easier to understand than simple string modifications? Nice try...

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                G 1 Reply Last reply
                1
                • Christian EhrlicherC Christian Ehrlicher

                  @Gourmet said in Split QString generated from QGeoCoordinate.:

                  less to read means easer to read.

                  So you say me you can read a regexp (which you are not aware to create on your own so I guess you won't understand it later on) is easier to understand than simple string modifications? Nice try...

                  G Offline
                  G Offline
                  Gourmet
                  wrote on last edited by Gourmet
                  #9

                  @Christian-Ehrlicher regexp has one specific feature - it is much more easer to read and understand than to write. This is because of it's "encapsulation" - when you see it you already know what it means. Often it's not needed parse it when reading - just treat as whole thing.

                  1 Reply Last reply
                  0
                  • VRoninV Offline
                    VRoninV Offline
                    VRonin
                    wrote on last edited by
                    #10

                    I'll go a completely different way

                    QGeoCoordinate result(0.0,0.0,0.0);
                    const QRegularExpression dobleMatchingRegExp(QStringLiteral("[-+]?\\d*\\.?\\d+"));
                    QRegularExpressionMatchIterator i = dobleMatchingRegExp.globalMatch(s);
                    if(i.hasNext()) result.setLatitude(i.next().capturedRef(0).toDouble());
                    if(i.hasNext()) result.setLongitude(i.next().capturedRef(0).toDouble());
                    if(i.hasNext()) result.setAltitude(i.next().capturedRef(0).toDouble());
                    return result;
                    

                    Sources:

                    • https://www.regular-expressions.info/floatingpoint.html
                    • https://doc.qt.io/qt-5/qregularexpression.html#global-matching

                    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                    ~Napoleon Bonaparte

                    On a crusade to banish setIndexWidget() from the holy land of Qt

                    G 1 Reply Last reply
                    2
                    • VRoninV VRonin

                      I'll go a completely different way

                      QGeoCoordinate result(0.0,0.0,0.0);
                      const QRegularExpression dobleMatchingRegExp(QStringLiteral("[-+]?\\d*\\.?\\d+"));
                      QRegularExpressionMatchIterator i = dobleMatchingRegExp.globalMatch(s);
                      if(i.hasNext()) result.setLatitude(i.next().capturedRef(0).toDouble());
                      if(i.hasNext()) result.setLongitude(i.next().capturedRef(0).toDouble());
                      if(i.hasNext()) result.setAltitude(i.next().capturedRef(0).toDouble());
                      return result;
                      

                      Sources:

                      • https://www.regular-expressions.info/floatingpoint.html
                      • https://doc.qt.io/qt-5/qregularexpression.html#global-matching
                      G Offline
                      G Offline
                      Gourmet
                      wrote on last edited by
                      #11

                      @VRonin so... cumbersome...

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved