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. How to convert and QLineEdit to QString and then to an array?

How to convert and QLineEdit to QString and then to an array?

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 5 Posters 1.1k 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.
  • N Offline
    N Offline
    Nycollas
    wrote on last edited by
    #1

    I want to the user enter the positions (x,y) on the QLineEdit interface like this:
    {{1,2},{4,7},{3,7.2}}
    and convert this to an array. I get the text using and QString but now I don't know how to read the text and transform it into na array.
    I created a struct called position to put the values in and then do my code, similar like this:

    struct position{
    double xy[2]
    };

    So in the end I want the struct to look like:
    position.xy[1] = {1,2}
    position.xy[2] = {4,7}
    position.xy[3] = {3,7.2}

    I don't know if this is the easiest way to do it, so I'm accepting suggestions. Thank you all guys!!

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

      QRegularExpression using R"***({{([-+]?\d*\.?\d+),([-+]?\d*\.?\d+)},{([-+]?\d*\.?\d+),([-+]?\d*\.?\d+)},{([-+]?\d*\.?\d+),([-+]?\d*\.?\d+)}})***" as the pattern.
      You'll have each number in a different capturing group

      "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

      JonBJ 1 Reply Last reply
      4
      • VRoninV VRonin

        QRegularExpression using R"***({{([-+]?\d*\.?\d+),([-+]?\d*\.?\d+)},{([-+]?\d*\.?\d+),([-+]?\d*\.?\d+)},{([-+]?\d*\.?\d+),([-+]?\d*\.?\d+)}})***" as the pattern.
        You'll have each number in a different capturing group

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #3

        @VRonin
        Sorry, but what are your leading & trailing *** for? :confused:

        Side-question: To avoid having to repeat the number segments, I know you can use {1,3}-type repeater in reg exes. I guess you can't then get separate capture groups out of them?

        mrjjM 1 Reply Last reply
        1
        • JonBJ JonB

          @VRonin
          Sorry, but what are your leading & trailing *** for? :confused:

          Side-question: To avoid having to repeat the number segments, I know you can use {1,3}-type repeater in reg exes. I guess you can't then get separate capture groups out of them?

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          @JonB
          Hi
          Its a c++ raw string literal. its removes the need for escaping.
          the *** is a custom delimiter.
          https://en.cppreference.com/w/cpp/language/string_literal

          1 Reply Last reply
          3
          • fcarneyF Offline
            fcarneyF Offline
            fcarney
            wrote on last edited by
            #5

            @Nycollas said in How to convert and QLineEdit to QString and then to an array?:

            I want to the user enter the positions (x,y) on the QLineEdit interface like this:
            {{1,2},{4,7},{3,7.2}}

            Is this a syntax you have chosen? Or can you change it?
            At first glance I would do a string replace of "{}" characters for "[]" and just evaluate it using the JSON functions from Qt. Then it is just a matter of cycling through the lists and filling in your structure.

            C++ is a perfectly valid school of magic.

            1 Reply Last reply
            3

            • Login

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