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.
  • ManiRonM Offline
    ManiRonM Offline
    ManiRon
    wrote on last edited by
    #1

    I have a screen in which i will display the numbers pressed(Eg : similar to a calculator) in a Qlineedit . But i want to check that the number entered in Qlineedit should be from the range of 4400 to 5000 and it should accept any decimal value from this range (for eg it should accept 4500.5 and it should not accept 1111 or any number that starts with 1,2,3,6,7,8,9,0) how can we do this.

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      You can use QLineEdit::setValidator to achieve this, together with QDoubleValidator or QIntValidator.

      Or, alternatively, use QSpinBox/ QDoubleSpinbox.

      (Z(:^

      ManiRonM 1 Reply Last reply
      2
      • sierdzioS sierdzio

        You can use QLineEdit::setValidator to achieve this, together with QDoubleValidator or QIntValidator.

        Or, alternatively, use QSpinBox/ QDoubleSpinbox.

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

        @sierdzio i dont want to use spin box . and i have tried using

        QDoubleValidator *validator = new QDoubleValidator(4400.0, 5000.0,3,ui->TxtInput);
        validator->setNotation(QDoubleValidator::StandardNotation);
        ui->TxtInput->setValidator(validator);

        and i want the line edit to only accept the data from range 4400 - 5000) . How to do it.

        jsulmJ 1 Reply Last reply
        0
        • ManiRonM ManiRon

          @sierdzio i dont want to use spin box . and i have tried using

          QDoubleValidator *validator = new QDoubleValidator(4400.0, 5000.0,3,ui->TxtInput);
          validator->setNotation(QDoubleValidator::StandardNotation);
          ui->TxtInput->setValidator(validator);

          and i want the line edit to only accept the data from range 4400 - 5000) . How to do it.

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

          @ManiRon So, the code above does not work?

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

          ManiRonM 1 Reply Last reply
          1
          • jsulmJ jsulm

            @ManiRon So, the code above does not work?

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

            @jsulm yes it doesnt work

            jsulmJ 1 Reply Last reply
            0
            • ManiRonM ManiRon

              @jsulm yes it doesnt work

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

              @ManiRon Would be nice to know in what way it does not work. You can't enter values you want to enter? You can enter values which should not be allowed? ...?

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

              ManiRonM 1 Reply Last reply
              3
              • jsulmJ jsulm

                @ManiRon Would be nice to know in what way it does not work. You can't enter values you want to enter? You can enter values which should not be allowed? ...?

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

                @jsulm

                yes for example i was able to enter 1111, but as per the condition line edit shouldnt accept it right .

                jsulmJ 1 Reply Last reply
                0
                • ManiRonM ManiRon

                  @jsulm

                  yes for example i was able to enter 1111, but as per the condition line edit shouldnt accept it right .

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

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

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

                  ManiRonM 1 Reply Last reply
                  1
                  • sierdzioS Offline
                    sierdzioS Offline
                    sierdzio
                    Moderators
                    wrote on last edited by
                    #9

                    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.

                    (Z(:^

                    ManiRonM 3 Replies Last reply
                    1
                    • 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
                      #10

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

                      jsulmJ 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
                        #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