I am trying to set up a QIntValidator to validate input on a QLineEdit. This is what I did:
userInput = new QLineEdit("1");
userInput->setValidator(new QIntValidator ( 1, 20, this ) );
This appears to work: it does not allow any letters in. However, I can type in 0
, which is out of range, and I can also type in numbers like 21
, 39
and 80
, all out of range of 1-20. Basically, I expect that the QIntValidator will only allow number inputs in the range 1-20, but, instead, I find that it allows all nonnegative number inputs less than 100 (I can also do 00000
.)
Why is the QIntValidator not working as I expected it to and how do I fix it?
The 0
is accepted as QValidator::Intermediate
state, because the user may intend to type e.g. 05
, which would be valid. You won't be able to actually input the undesired value. After you press Return or move focus from the widget, having 0
in the input field, the value should return to the original (well, at least spinboxes behave so, not sure about QLineEdit
).