I am validating my TextBox to take only numbers between 1-75. I am able to do this by using below code :
QValidator *validator = new QIntValidator(1, 75, this);
QLineEdit *edit = new QLineEdit(this);
edit->setValidator(validator);
But now the problem is it takes zero also. I want to avoid Zero Any help is appreciated.
You are not currently validating: when you set a QValidator you are just filtering characters to those which could compose a valid input.
In order to do the actual validation, you have to check that QLineEdit::hasAcceptableInput() returns true.
For example, in a slot connected to the textChanged() signal, you could, depending on the value of the acceptableInput property, enable/disable the button submitting the data, preventing the user to go further with an invalid value, and/or change the text color (red for invalid).
For numerical values, you can also use a QSpinBox instead of QLineEdit. You can define its range (min/max), and its behavior if the value is not valid when the editing finishes (with QSpinBox::setCorrectionMode).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With