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. Qt validator doesnt work like described in docs
Forum Updated to NodeBB v4.3 + New Features

Qt validator doesnt work like described in docs

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 4 Posters 362 Views 4 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.
  • M Offline
    M Offline
    masa4
    wrote on last edited by
    #1

    QIntValidator from docs:

    QValidator *validator = new QIntValidator(100, 999, this);
    QLineEdit *edit = new QLineEdit(this);
    
    // the edit lineedit will only accept integers between 100 and 999
    edit->setValidator(validator);
    

    What I am using in my code:

    ui->lineEdit->setValidator(new QIntValidator(1,1000,this));
    

    I was expecting that i can input this lineEdit between 1 to 1000 but i can input from 0 to 9999. Any idea why?

    C 1 Reply Last reply
    0
    • M masa4

      QIntValidator from docs:

      QValidator *validator = new QIntValidator(100, 999, this);
      QLineEdit *edit = new QLineEdit(this);
      
      // the edit lineedit will only accept integers between 100 and 999
      edit->setValidator(validator);
      

      What I am using in my code:

      ui->lineEdit->setValidator(new QIntValidator(1,1000,this));
      

      I was expecting that i can input this lineEdit between 1 to 1000 but i can input from 0 to 9999. Any idea why?

      C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #2

      @masa4 The validator work exactly as in the docs. Read examples in the documentation following you one you quoted, and the paragraph following:

      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.

      For your validator, with 9999 typed into the line edit, QLineEdit::hasAcceptableInput() will return false, and QValidator::validate() will return QValidator::Intermediate.

      #include <QApplication>
      #include <QDebug>
      #include <QLineEdit>
      #include <QIntValidator>
      
      int main(int argc, char **argv)
      {
              QApplication app(argc, argv);
      
              QString test("9999");
      
              QLineEdit edit;
              QIntValidator *validator = new QIntValidator(1, 1000, &edit);
              edit.setValidator(validator);
              edit.setText(test);
      
              int pos;
              qDebug() << validator->validate(test, pos);
              qDebug() << edit.hasAcceptableInput();
      
              return app.exec();
      }
      
      M 1 Reply Last reply
      2
      • C ChrisW67

        @masa4 The validator work exactly as in the docs. Read examples in the documentation following you one you quoted, and the paragraph following:

        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.

        For your validator, with 9999 typed into the line edit, QLineEdit::hasAcceptableInput() will return false, and QValidator::validate() will return QValidator::Intermediate.

        #include <QApplication>
        #include <QDebug>
        #include <QLineEdit>
        #include <QIntValidator>
        
        int main(int argc, char **argv)
        {
                QApplication app(argc, argv);
        
                QString test("9999");
        
                QLineEdit edit;
                QIntValidator *validator = new QIntValidator(1, 1000, &edit);
                edit.setValidator(validator);
                edit.setText(test);
        
                int pos;
                qDebug() << validator->validate(test, pos);
                qDebug() << edit.hasAcceptableInput();
        
                return app.exec();
        }
        
        M Offline
        M Offline
        masa4
        wrote on last edited by
        #3

        @ChrisW67 Thanks for clarifying the issue. So do you know any simple solution to prevent user inputting intermadiate values?

        C 1 Reply Last reply
        0
        • M masa4

          @ChrisW67 Thanks for clarifying the issue. So do you know any simple solution to prevent user inputting intermadiate values?

          C Offline
          C Offline
          ChrisW67
          wrote on last edited by
          #4

          @masa4 Are you trying to use the intermediate values as they type it? You should consider acting only on the returnPressed() or editingFinished() signals.

          M 1 Reply Last reply
          0
          • C ChrisW67

            @masa4 Are you trying to use the intermediate values as they type it? You should consider acting only on the returnPressed() or editingFinished() signals.

            M Offline
            M Offline
            masa4
            wrote on last edited by
            #5

            @ChrisW67 I want to prevent user to input anyting outside the 1-1000. User should not eligible to input enter 0, or anything bigger to 1000 and negative values. Right now user can't input negative but can input 0 and numbers that bigger than 1000.

            SGaistS 1 Reply Last reply
            0
            • M masa4

              @ChrisW67 I want to prevent user to input anyting outside the 1-1000. User should not eligible to input enter 0, or anything bigger to 1000 and negative values. Right now user can't input negative but can input 0 and numbers that bigger than 1000.

              SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Hi,

              Out of curiosity, why not use a QSpinBox ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              M 1 Reply Last reply
              1
              • SGaistS SGaist

                Hi,

                Out of curiosity, why not use a QSpinBox ?

                M Offline
                M Offline
                masa4
                wrote on last edited by
                #7

                @SGaist Does it have integrated validator?

                C 1 Reply Last reply
                0
                • M masa4

                  @SGaist Does it have integrated validator?

                  C Offline
                  C Offline
                  CPPUIX
                  wrote on last edited by
                  #8

                  @masa4 It has setMaximum and setMinimum, which seems to be exactly what you need

                  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