Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. How to bind QString value in perticular range ?
Qt 6.12 LTS is out! See what's new in the release blog

How to bind QString value in perticular range ?

Scheduled Pinned Locked Moved Solved Mobile and Embedded
14 Posts 6 Posters 2.5k 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.
  • JonBJ JonB

    @Qt-embedded-developer said in How to bind QString value in perticular range ?:

    i want to do operation on string which has value between "000" to "999".
    NOW when unknown string come at that time i need to replace it with "000".

    A "lazy" way would be

    if (! (str >= "000" && str <= "999"))
        str = "000";
    

    EDIT MY BAD, NOT ENOUGH COFFEE YET, I CORRECT MYSELF IN LATER POST....

    A more robust way would be to use QRegularExpression.

    J.HilkJ Offline
    J.HilkJ Offline
    J.Hilk
    Moderators
    wrote on last edited by
    #3

    @JonB said in How to bind QString value in perticular range ?:

    if (! (str >= "000" && str <= "999"))
    str = "000";

    I'm disgusted, that this actually works, feels like Python/JavaScript

    I wonder how the >= is actually implemented 🤔


    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


    Q: What's that?
    A: It's blue light.
    Q: What does it do?
    A: It turns blue.

    JonBJ JKSHJ 2 Replies Last reply
    1
    • J.HilkJ J.Hilk

      @JonB said in How to bind QString value in perticular range ?:

      if (! (str >= "000" && str <= "999"))
      str = "000";

      I'm disgusted, that this actually works, feels like Python/JavaScript

      I wonder how the >= is actually implemented 🤔

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

      @J-Hilk
      Well I never test anything, I just type in my proposed answers.

      I'm lost, why disgusted? What do you mean about "how >= is implemented"? It's a straight string comparison, and digits are guaranteed to be consecutive characters or code points or whatever they call them. It's not going to convert to the number, if that's what you mean.

      Just to be clear, if the input were, say, 0001 this test would treat that as valid, and not alter to 000, which a regular expression could correctly catch. But if the OP knows the input is always 3 characters it suffices. Of course I would use the reg ex, but the OP might not fancy that....

      J.HilkJ 1 Reply Last reply
      1
      • T Offline
        T Offline
        TheGringerEye
        wrote on last edited by
        #5
        QString test("63");
        QIntValidator valid(0, 999);
        int pos;
        if (QValidator::State::Acceptable != valid.validate(test, pos))
        {
            test = QLatin1Char('0');
        }
        test = test.sprintf("%03i", test.toInt());
        

        I try to learn English.

        Q 1 Reply Last reply
        0
        • J.HilkJ J.Hilk

          @JonB said in How to bind QString value in perticular range ?:

          if (! (str >= "000" && str <= "999"))
          str = "000";

          I'm disgusted, that this actually works, feels like Python/JavaScript

          I wonder how the >= is actually implemented 🤔

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

          @J-Hilk said in How to bind QString value in perticular range ?:

          I'm disgusted, that this actually works, feels like Python/JavaScript

          It doesn't fully work. "1111" >= "000" and "1111" <= "999" are both true.

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

          JonBJ 1 Reply Last reply
          1
          • T TheGringerEye
            QString test("63");
            QIntValidator valid(0, 999);
            int pos;
            if (QValidator::State::Acceptable != valid.validate(test, pos))
            {
                test = QLatin1Char('0');
            }
            test = test.sprintf("%03i", test.toInt());
            
            Q Offline
            Q Offline
            Qt embedded developer
            wrote on last edited by
            #7

            @TheGringerEye it gives below error
            error: 'QValidator::State' is not a class or namespace
            if (QValidator::State::Acceptable != valid.validate(test, pos))
            ^

            T 1 Reply Last reply
            0
            • Q Qt embedded developer

              @TheGringerEye it gives below error
              error: 'QValidator::State' is not a class or namespace
              if (QValidator::State::Acceptable != valid.validate(test, pos))
              ^

              T Offline
              T Offline
              TheGringerEye
              wrote on last edited by
              #8

              @Qt-embedded-developer
              #include <QIntValidator>

              I try to learn English.

              Q 1 Reply Last reply
              0
              • JonBJ JonB

                @J-Hilk
                Well I never test anything, I just type in my proposed answers.

                I'm lost, why disgusted? What do you mean about "how >= is implemented"? It's a straight string comparison, and digits are guaranteed to be consecutive characters or code points or whatever they call them. It's not going to convert to the number, if that's what you mean.

                Just to be clear, if the input were, say, 0001 this test would treat that as valid, and not alter to 000, which a regular expression could correctly catch. But if the OP knows the input is always 3 characters it suffices. Of course I would use the reg ex, but the OP might not fancy that....

                J.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on last edited by
                #9

                @JonB Well, that would make a bit more sense, true

                TBH I expected this to default to a length comparison or pointer/addresses

                Don't mind me, I'm way behind on my daily coffee doses


                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                1 Reply Last reply
                0
                • JKSHJ JKSH

                  @J-Hilk said in How to bind QString value in perticular range ?:

                  I'm disgusted, that this actually works, feels like Python/JavaScript

                  It doesn't fully work. "1111" >= "000" and "1111" <= "999" are both true.

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

                  @JKSH

                  It doesn't fully work. "1111" >= "000" and "1111" <= "999" are both true.

                  Again I'm not with you. That is a 4-digit string. Just like my 0001 example above. I stated that would be treated as acceptable, where the OP might want unacceptable. I said my suggestion is "lazy" because of this, it works assuming the input is 3 characters long.

                  Equally @TheGringerEye's solution treats, say, a 2-digit 12 as valid, we don't know what the OP wants done with that. These solutions depend on precisely what the OP wants treated how....

                  If the OP wants the "lazy" way it could be improved with

                  if (! (str.length() == 3 && str >= "000" && str <= "999"))
                      str = "000";
                  

                  EDIT MY BAD, NOT ENOUGH COFFEE YET, I CORRECT MYSELF IN LATER POST....

                  T 1 Reply Last reply
                  1
                  • JonBJ JonB

                    @JKSH

                    It doesn't fully work. "1111" >= "000" and "1111" <= "999" are both true.

                    Again I'm not with you. That is a 4-digit string. Just like my 0001 example above. I stated that would be treated as acceptable, where the OP might want unacceptable. I said my suggestion is "lazy" because of this, it works assuming the input is 3 characters long.

                    Equally @TheGringerEye's solution treats, say, a 2-digit 12 as valid, we don't know what the OP wants done with that. These solutions depend on precisely what the OP wants treated how....

                    If the OP wants the "lazy" way it could be improved with

                    if (! (str.length() == 3 && str >= "000" && str <= "999"))
                        str = "000";
                    

                    EDIT MY BAD, NOT ENOUGH COFFEE YET, I CORRECT MYSELF IN LATER POST....

                    T Offline
                    T Offline
                    TheGringerEye
                    wrote on last edited by
                    #11

                    @JonB Yes, if you need not to skip "0001", then you need to add a check for the length of the string

                    I try to learn English.

                    1 Reply Last reply
                    0
                    • JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by JonB
                      #12

                      EDIT MY BAD, NOT ENOUGH COFFEE YET, I CORRECT MYSELF, SORRY....

                      Sorry, not thinking clearly at all.

                      If input can contain non-digits, my "lazy":

                      str >= "000" && str <= "999"
                      

                      would not be right at all, 0AZ would be acceptable!

                      So either convert to a number and compare numerically (e.g. QIntValidator or other) or do a regular expression, depending on what is wanted/not wanted....

                      1 Reply Last reply
                      0
                      • T TheGringerEye

                        @Qt-embedded-developer
                        #include <QIntValidator>

                        Q Offline
                        Q Offline
                        Qt embedded developer
                        wrote on last edited by
                        #13

                        @TheGringerEye said in How to bind QString value in perticular range ?:

                        #include <QIntValidator>

                        after adding header file still i get error QValidator::State is not a class or name space

                        jsulmJ 1 Reply Last reply
                        0
                        • Q Qt embedded developer

                          @TheGringerEye said in How to bind QString value in perticular range ?:

                          #include <QIntValidator>

                          after adding header file still i get error QValidator::State is not a class or name space

                          jsulmJ Online
                          jsulmJ Online
                          jsulm
                          Lifetime Qt Champion
                          wrote on last edited by
                          #14

                          @Qt-embedded-developer It should be

                          if (QValidator::Acceptable != valid.validate(test, pos))
                          

                          Easy to find out actually by reading documentation and considering how C++ enums work...

                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          4

                          • Login

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