Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QValidator in Qt?

Tags:

qt

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.

like image 343
Bokambo Avatar asked Oct 23 '25 17:10

Bokambo


1 Answers

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).

like image 145
alexisdm Avatar answered Oct 27 '25 03:10

alexisdm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!