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 check the range of data entered in Qlineedit
Forum Updated to NodeBB v4.3 + New Features

how to check the range of data entered in Qlineedit

Scheduled Pinned Locked Moved Unsolved General and Desktop
20 Posts 5 Posters 5.4k 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.
  • sierdzioS sierdzio

    After user edits the line edit, check acceptableInput property. If it is not acceptable, you can reset the value.

    My guess is that 1111 is seen as an intermediate value - because the user can still change it to 4911.11 which is acceptable.

    ManiRonM Offline
    ManiRonM Offline
    ManiRon
    wrote on last edited by
    #11

    @sierdzio can we not check for the input at the time of entry itself

    1 Reply Last reply
    0
    • sierdzioS sierdzio

      After user edits the line edit, check acceptableInput property. If it is not acceptable, you can reset the value.

      My guess is that 1111 is seen as an intermediate value - because the user can still change it to 4911.11 which is acceptable.

      ManiRonM Offline
      ManiRonM Offline
      ManiRon
      wrote on last edited by
      #12

      @sierdzio i checked the acceptable input and it returns 0

      1 Reply Last reply
      0
      • jsulmJ jsulm

        @ManiRon What I can see with this validator is that you can only enter numbers but not letters. But you can enter any numbers. Not sure why it behaves this way. Could be a bug.

        ManiRonM Offline
        ManiRonM Offline
        ManiRon
        wrote on last edited by
        #13

        @jsulm will there be any way i can validate when an input is entered in a QLine edit

        J.HilkJ 1 Reply Last reply
        0
        • ManiRonM ManiRon

          @sierdzio how to make it to accept only between 4500 - 5000

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

          @ManiRon said in how to check the range of data entered in Qlineedit:

          how to make it to accept only between 4500 - 5000

          In the way @sierdzio explained: call acceptableInput() when user finishes typing (pressing enter or some button, moving focus to another widget), if it returns false reset the value in the editor and ask user to input valid value.
          The problem is that the final value is available when the user finished entering it, but how can the editor know when the user is finished? For example you're entering 45 - this is not acceptable value, right? But if you continue and enter 4501 it is valid, right?

          "i checked the acceptable input and it returns 0" - which is false, means not acceptable...

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

          1 Reply Last reply
          3
          • ManiRonM ManiRon

            @jsulm will there be any way i can validate when an input is entered in a QLine edit

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

            @ManiRon
            look at that, I'm can do magic after all, a little bit at least ;-)

            int main(int argc, char *argv[])
            {
                QApplication a(argc, argv);
            
                QLineEdit *le = new QLineEdit();
                le->setInputMask(QString("9999"));
                le->setText("4500");
                QRegularExpression re("(4[5-8][0-9]{2}|49[0-8][0-9]|499[0-9]|5000)");
                QRegularExpressionValidator *validator = new QRegularExpressionValidator(re);
                le->setValidator(validator);
            
                le->show();
            
                return a.exec();
            }
            

            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 1 Reply Last reply
            1
            • J.HilkJ J.Hilk

              @ManiRon
              look at that, I'm can do magic after all, a little bit at least ;-)

              int main(int argc, char *argv[])
              {
                  QApplication a(argc, argv);
              
                  QLineEdit *le = new QLineEdit();
                  le->setInputMask(QString("9999"));
                  le->setText("4500");
                  QRegularExpression re("(4[5-8][0-9]{2}|49[0-8][0-9]|499[0-9]|5000)");
                  QRegularExpressionValidator *validator = new QRegularExpressionValidator(re);
                  le->setValidator(validator);
              
                  le->show();
              
                  return a.exec();
              }
              
              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #16

              @J.Hilk
              I do not mean to be disrespectful, but IMHO showing someone how to validate an intended input numeric range via a (complex) regular expression such as yours which tries to capture the acceptable input as a string limited to certain digit patterns is not a good idea. It is difficult to write/maintain, and is far too brittle for possible future changes in the range accepted (which may well not be doable by reg exs at all). It should be done via a function which converts the characters to a number and validates that.

              J.HilkJ 1 Reply Last reply
              0
              • JonBJ JonB

                @J.Hilk
                I do not mean to be disrespectful, but IMHO showing someone how to validate an intended input numeric range via a (complex) regular expression such as yours which tries to capture the acceptable input as a string limited to certain digit patterns is not a good idea. It is difficult to write/maintain, and is far too brittle for possible future changes in the range accepted (which may well not be doable by reg exs at all). It should be done via a function which converts the characters to a number and validates that.

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

                @JonB hey, that's quite literally the 3rd time I used a regular expression and like both times before I used the Internet to get the actual expression:
                http://gamon.webfactional.com/regexnumericrangegenerator/

                This is all magic hand waving for me.

                And I agree with your point, a custom/sub classed QLineEdit is the better way to do it, in my opinion at least.


                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 1 Reply Last reply
                1
                • J.HilkJ J.Hilk

                  @JonB hey, that's quite literally the 3rd time I used a regular expression and like both times before I used the Internet to get the actual expression:
                  http://gamon.webfactional.com/regexnumericrangegenerator/

                  This is all magic hand waving for me.

                  And I agree with your point, a custom/sub classed QLineEdit is the better way to do it, in my opinion at least.

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

                  @J.Hilk
                  Huh? You are a "Moderator" here, so I expect you to be god-like in your knowledge/experience --- how can you possibly only have used a reg ex 3 times, I have been using them for more than a quarter of a century! :)

                  Your reg ex is (probably) perfectly correct for capturing the precise requirement expressed by the OP, I am not saying it isn't. But it's like pushing a square peg into a round hole here: it fits but it's wobbly, there are gaps round the edges, and once it's forced in it will be difficult to get it back out :) The OP should use a round peg instead!

                  J.HilkJ 1 Reply Last reply
                  1
                  • JonBJ JonB

                    @J.Hilk
                    Huh? You are a "Moderator" here, so I expect you to be god-like in your knowledge/experience --- how can you possibly only have used a reg ex 3 times, I have been using them for more than a quarter of a century! :)

                    Your reg ex is (probably) perfectly correct for capturing the precise requirement expressed by the OP, I am not saying it isn't. But it's like pushing a square peg into a round hole here: it fits but it's wobbly, there are gaps round the edges, and once it's forced in it will be difficult to get it back out :) The OP should use a round peg instead!

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

                    @JonB said in how to check the range of data entered in Qlineedit:

                    @J.Hilk
                    Huh? You are a "Moderator" here, so I expect you to be god-like in your knowledge/experience ---

                    I'm new :P

                    how can you possibly only have used a reg ex 3 times, I have been using them for more than a quarter of a century! :)

                    I do use it more often, but its a fixed expression I haven't touch since the beginning, I use it for Date & Time edits -> 2 RegExp and the preciously posted one as the 3rd ;)

                    Your reg ex is (probably) perfectly correct for capturing the precise requirement expressed by the OP, I am not saying it isn't.

                    It actually is not, you can't enter 5000, only 4500 to 4999. Because 0 is not allowed at the 2nd place -> you can't enter 5 at the first place.

                    I should have tested more before posting....


                    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 1 Reply Last reply
                    0
                    • J.HilkJ J.Hilk

                      @JonB said in how to check the range of data entered in Qlineedit:

                      @J.Hilk
                      Huh? You are a "Moderator" here, so I expect you to be god-like in your knowledge/experience ---

                      I'm new :P

                      how can you possibly only have used a reg ex 3 times, I have been using them for more than a quarter of a century! :)

                      I do use it more often, but its a fixed expression I haven't touch since the beginning, I use it for Date & Time edits -> 2 RegExp and the preciously posted one as the 3rd ;)

                      Your reg ex is (probably) perfectly correct for capturing the precise requirement expressed by the OP, I am not saying it isn't.

                      It actually is not, you can't enter 5000, only 4500 to 4999. Because 0 is not allowed at the 2nd place -> you can't enter 5 at the first place.

                      I should have tested more before posting....

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

                      @J.Hilk
                      That's why I said it would be brittle, and there are likely to be certain ranges for which no pattern is suitable (haven't thought it through, just assuming so), or at least difficult.

                      It actually is not, you can't enter 5000, only 4500 to 4999. Because 0 is not allowed at the 2nd place -> you can't enter 5 at the first place.

                      No, 5000 is OK, because you are using | and you have |5000 as an explicit allowance.

                      I now see your link is to somewhere where they generate a numeric range reg ex for you. IMHO that should come with a health warning! BTW I randomly typed in range 123456 to 7980678 and it gives:

                      (12345[6-9]|1234[6-9][0-9]|123[5-9][0-9]{2}|12[4-9][0-9]{3}|1[3-9][0-9]{4}|[2-9][0-9]{5}|[1-6][0-9]{6}|7[0-8][0-9]{5}|79[0-7][0-9]{4}|7980[0-5][0-9]{2}|79806[0-6][0-9]|798067[0-8])
                      

                      which again may well be correct but is hardly nice reading, is it? :)

                      Also btw, assuming you use something like Qt Creator for your coding, haven't you used reg exs there from time to time when searching your code? That counts as reg ex experience too!

                      1 Reply Last reply
                      1

                      • Login

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