Unsolved QIntValidators dont work for me
-
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
-
@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?
-
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
-
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 ?
-
@nullbuil7
In the case of the docs example or your own code, what doesedit->validate()
return?Intermediate
? Are you in the situation described in another example forQIntValidator 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.
-
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
-
@nullbuil7 yes, i found it(QIntValidator, QDoubleValidtor) doesn't work too. my QLineEdit top can be input "999" when i set QIntValidator(0, 500)
-
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(); }
-
See also https://bugreports.qt.io/browse/QTBUG-76649 and other related bug reports why it's the way it is.
-
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); -
@DrewB Does this behave any differently from the clearer
QIntValidator
?