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. QIntValidators dont work for me
Forum Updated to NodeBB v4.3 + New Features

QIntValidators dont work for me

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 7 Posters 2.9k 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.
  • N nullbuil7

    for example a part of my program code is:

        // decEdit is QLineEdit
        QIntValidator* decValidator =
                new QIntValidator(0, 255, decEdit);
        decEdit->setValidator(decValidator);
    

    this does not set any restrictions other than it should be three numbers

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

    @nullbuil7
    Here is the example from the 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);
    

    Are you saying this does not work as documented?

    1 Reply Last reply
    1
    • N Offline
      N Offline
      nullbuil7
      wrote on last edited by
      #3

      yes it doesn't work. if you say i want top to be 4000 it can go up to 9999. i don't know why

      JonBJ X 2 Replies Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #4

        Hi,

        The fact that you can enter higher value does not make sais value valid.

        By the way, which version of Qt are you using ?

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

        1 Reply Last reply
        0
        • N nullbuil7

          yes it doesn't work. if you say i want top to be 4000 it can go up to 9999. i don't know why

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

          @nullbuil7
          In the case of the docs example or your own code, what does edit->validate() return? Intermediate? Are you in the situation described in another example for QIntValidator v(100, 900, this); where it says:

          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.

          because this ties with your maximum of 255 and your "other than it should be three numbers [digits]"? When you know the user has finished typing you need the validator to return Acceptable, not Intermediate (or Invalid), for the input to be valid. A validator is not just about preventing the user from typing characters, it's also about checking the validity of the characters it has allowed the user to type, so two aspects.

          1 Reply Last reply
          0
          • N Offline
            N Offline
            nullbuil7
            wrote on last edited by
            #6

            i searched the web and made my own validator but i don't know how to use it. what should i do?

            #ifndef VALIDATOR_H
            #define VALIDATOR_H
            #include <QObject>
            #include <QValidator>
            
            class binValidator : public QValidator
            {
                Q_OBJECT
            public:
                explicit binValidator(QObject *parent = nullptr);
                virtual State validate(QString & input, int & pos) const override
                {
                    if (input.isEmpty())
                        return Acceptable;
            
                    bool b;
                    int val = input.toInt(&b);
            
                    if ((b == true) && (val >= 0) && (val <= 255))
                    {
                        return Acceptable;
                    }
                    return Invalid;
                }
            };
            
            #endif
            
            1 Reply Last reply
            0
            • N nullbuil7

              yes it doesn't work. if you say i want top to be 4000 it can go up to 9999. i don't know why

              X Offline
              X Offline
              Xiao Yang
              wrote on last edited by
              #7

              @nullbuil7 yes, i found it(QIntValidator, QDoubleValidtor) doesn't work too. my QLineEdit top can be input "999" when i set QIntValidator(0, 500)

              1 Reply Last reply
              0
              • VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by
                #8

                Indeed it has been explicitly coded to return intermidiate in that case.

                You can use this:

                class HarshIntValidator : public QIntValidator{
                    Q_OBJECT
                    Q_DISABLE_COPY(HarshIntValidator)
                public:
                    using QIntValidator::QIntValidator;
                    State validate(QString & input, int & pos) const override{
                        const State originalRes = QIntValidator::validate(input,pos);
                        if(originalRes==Intermediate){
                            const auto extracted = locale().toLongLong(input);
                            if(extracted>0){
                                if(extracted>top() && -extracted<bottom())
                                    return Invalid;
                            }
                            else if(extracted<bottom())
                                return Invalid;
                        }
                        return  originalRes;
                    }
                };
                

                you can use it exactly as you use QIntValidator:

                int main(int argc, char *argv[])
                {
                    QApplication a(argc, argv);
                    QLineEdit w;
                    HarshIntValidator valid(0,255);
                    w.setValidator(&valid);
                    w.show();
                    return a.exec();
                }
                

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                1 Reply Last reply
                3
                • Christian EhrlicherC Online
                  Christian EhrlicherC Online
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #9

                  See also https://bugreports.qt.io/browse/QTBUG-76649 and other related bug reports why it's the way it is.

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

                  1 Reply Last reply
                  3
                  • D Offline
                    D Offline
                    DrewB
                    wrote on last edited by
                    #10

                    To limit 0 to 255 you can use QRegularExpressionValidator instead of QIntValidator

                    QRegularExpressionValidator* decValidator = new QRegularExpressionValidator(QRegularExpression("([01][0-9][0-9]|2[0-4][0-9]|25[0-5])"), decEdit);
                    decEdit->setValidator(decValidator);

                    JonBJ 1 Reply Last reply
                    0
                    • D DrewB

                      To limit 0 to 255 you can use QRegularExpressionValidator instead of QIntValidator

                      QRegularExpressionValidator* decValidator = new QRegularExpressionValidator(QRegularExpression("([01][0-9][0-9]|2[0-4][0-9]|25[0-5])"), decEdit);
                      decEdit->setValidator(decValidator);

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

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

                      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