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. Can't understand how to work with QIntValidator

Can't understand how to work with QIntValidator

Scheduled Pinned Locked Moved Unsolved General and Desktop
15 Posts 6 Posters 3.3k 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.
  • Christian EhrlicherC Offline
    Christian EhrlicherC Offline
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on last edited by
    #4

    You can see that 99 is an intermediate state:

      QValidator *validator = new QIntValidator(1,10);
      QLineEdit *le = new QLineEdit;
      QObject::connect(le, &QLineEdit::textChanged, le, [&]() {
        QString s = le->text();
        int pos = 0;
        qDebug() << validator->validate(s, pos);
      });
      le->setValidator(validator);
      le->show();
    

    If you don't want this, you have to derive from QIntValidator and add the appropriate fixups. Or use a QSpinBox.

    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
    Visit the Qt Academy at https://academy.qt.io/catalog

    B 1 Reply Last reply
    2
    • B Bol4onok

      @Christian-Ehrlicher Yes, my bad, i use thath code:
      QValidator *validator = new QIntValidator(1,10);
      ui->lineLucky->setValidator(validator);
      As I understand it, with this minimum and maximum value, I can only enter 1 to 10, but in fact I can enter 1 to 99.

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by JonB
      #5

      @Bol4onok said in Can't understand how to work with QIntValidator:

      As I understand it, with this minimum and maximum value, I can only enter 1 to 10, but in fact I can enter 1 to 99.

      For the record: this is as described in https://doc.qt.io/qt-5/qintvalidator.html#details

      QIntValidator v(100, 900, this);
      Notice that the value 999 returns Intermediate. Values consisting of a number of digits equal to or less than the max value are considered intermediate. This is intended because the digit that prevents a number from being in range is not necessarily the last digit typed. This also means that an intermediate number can have leading zeros.

      You should find your 99 returns Intermediate. Follow @Christian-Ehrlicher's advice to resolve. If you just want a number input by user a QSpinBox is simplest and avoids this issue.

      1 Reply Last reply
      1
      • Christian EhrlicherC Christian Ehrlicher

        You can see that 99 is an intermediate state:

          QValidator *validator = new QIntValidator(1,10);
          QLineEdit *le = new QLineEdit;
          QObject::connect(le, &QLineEdit::textChanged, le, [&]() {
            QString s = le->text();
            int pos = 0;
            qDebug() << validator->validate(s, pos);
          });
          le->setValidator(validator);
          le->show();
        

        If you don't want this, you have to derive from QIntValidator and add the appropriate fixups. Or use a QSpinBox.

        B Offline
        B Offline
        Bol4onok
        wrote on last edited by
        #6

        @Christian-Ehrlicher I would be happy to use SpinBox, but i can't use it in this project, he ban

        1 Reply Last reply
        0
        • D Offline
          D Offline
          DrewB
          wrote on last edited by
          #7

          To limit 1 to 10 you could try using QRegularExpressionValidator instead of QIntValidator

          QRegularExpressionValidator* validator = new QRegularExpressionValidator(QRegularExpression("([1-9]|1[0])"), ui->lineLucky);
          ui->lineLucky->setValidator(validator);

          JonBJ 1 Reply Last reply
          0
          • D DrewB

            To limit 1 to 10 you could try using QRegularExpressionValidator instead of QIntValidator

            QRegularExpressionValidator* validator = new QRegularExpressionValidator(QRegularExpression("([1-9]|1[0])"), ui->lineLucky);
            ui->lineLucky->setValidator(validator);

            JonBJ Online
            JonBJ Online
            JonB
            wrote on last edited by
            #8

            @DrewB Does this behave any differently from the clearer QIntValidator?

            1 Reply Last reply
            0
            • J Offline
              J Offline
              j_lady
              wrote on last edited by
              #9

              So what is the purpose do the limits on QIntValidator serve? I can't see that they provide any functionality other than limiting the number of digits entered.

              Pl45m4P 1 Reply Last reply
              0
              • J j_lady

                So what is the purpose do the limits on QIntValidator serve? I can't see that they provide any functionality other than limiting the number of digits entered.

                Pl45m4P Offline
                Pl45m4P Offline
                Pl45m4
                wrote on last edited by
                #10

                @j_lady

                They dont limit the digits, the input is just Intermediate or Invalid then...
                How your Validator reacts on that is up to you


                If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                ~E. W. Dijkstra

                JonBJ 1 Reply Last reply
                0
                • Pl45m4P Pl45m4

                  @j_lady

                  They dont limit the digits, the input is just Intermediate or Invalid then...
                  How your Validator reacts on that is up to you

                  JonBJ Online
                  JonBJ Online
                  JonB
                  wrote on last edited by
                  #11

                  @Pl45m4
                  Not quite sure what you mean. FWIW they do limit the number of digits, e.g. as per above:

                  As I understand it, with this minimum and maximum value, I can only enter 1 to 10, but in fact I can enter 1 to 99.

                  Pl45m4P 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @Pl45m4
                    Not quite sure what you mean. FWIW they do limit the number of digits, e.g. as per above:

                    As I understand it, with this minimum and maximum value, I can only enter 1 to 10, but in fact I can enter 1 to 99.

                    Pl45m4P Offline
                    Pl45m4P Offline
                    Pl45m4
                    wrote on last edited by
                    #12

                    @JonB said in Can't understand how to work with QIntValidator:

                    Not quite sure what you mean

                    When your validator has (1, 42) bounds, 99 is still not an acceptable input even though it's also a 2-digit :))
                    (99 would also be Intermediate, AFAICS).
                    So it's not just "limiting the digits"


                    If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                    ~E. W. Dijkstra

                    JonBJ 1 Reply Last reply
                    1
                    • Pl45m4P Pl45m4

                      @JonB said in Can't understand how to work with QIntValidator:

                      Not quite sure what you mean

                      When your validator has (1, 42) bounds, 99 is still not an acceptable input even though it's also a 2-digit :))
                      (99 would also be Intermediate, AFAICS).
                      So it's not just "limiting the digits"

                      JonBJ Online
                      JonBJ Online
                      JonB
                      wrote on last edited by JonB
                      #13

                      @Pl45m4
                      I know. Again I don't see how that tallies with what you wrote:

                      They dont limit the digits, the input is just Intermediate or Invalid then...

                      But that is not true, they do limit the digits. To 2 in this case. Yes, 99 is not acceptable for a range of 1 to 42, but (for complicated/strange reasons) is does limit your your digits, to 2 here because that includes 42. It does not allow 3 digits, so it does limit them. That's what I was saying.

                      Pl45m4P 1 Reply Last reply
                      0
                      • JonBJ JonB

                        @Pl45m4
                        I know. Again I don't see how that tallies with what you wrote:

                        They dont limit the digits, the input is just Intermediate or Invalid then...

                        But that is not true, they do limit the digits. To 2 in this case. Yes, 99 is not acceptable for a range of 1 to 42, but (for complicated/strange reasons) is does limit your your digits, to 2 here because that includes 42. It does not allow 3 digits, so it does limit them. That's what I was saying.

                        Pl45m4P Offline
                        Pl45m4P Offline
                        Pl45m4
                        wrote on last edited by
                        #14

                        @JonB said in Can't understand how to work with QIntValidator:

                        It does not allow 3 digits, so it does limit them. That's what I was saying.

                        Yes, true, but for that purpose I don't need any validator. That can be checked/prevented way easier.
                        I was replying to

                        @j_lady said in Can't understand how to work with QIntValidator:

                        I can't see that they provide any functionality other than limiting the number of digits entered

                        which is kinda wrong, because it does not only do this.
                        Sure, when your desired input is a number from 1 to 10, it makes no sense to even check inputs like 88888.


                        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                        ~E. W. Dijkstra

                        1 Reply Last reply
                        0
                        • JonBJ Online
                          JonBJ Online
                          JonB
                          wrote on last edited by
                          #15

                          TBH I was pretty "unhappy" when I used Qt's int validator. I know they allow you to type the second 9 in 99 when the limit is 42 to do with because you could go back and delete a digit, or whatever its rule is. I can't put my finger on it, but I felt pretty sure that other validators I had used in software just wouldn't let you enter 99 for a limit of 42 in the first place rather than allowing it and then the user's input is illegal, and that seemed more intuitive to me in a UI....

                          1 Reply Last reply
                          0

                          • Login

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