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.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.
  • 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 Offline
        JonBJ Offline
        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 Offline
              JonBJ Offline
              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 Offline
                  JonBJ Offline
                  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 Offline
                      JonBJ Offline
                      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