Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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}}
    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!!



  • 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



  • @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?


  • Lifetime Qt Champion

    @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



  • @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.


Log in to reply