PDA

View Full Version : What to Expect from a QLineEdit Validator.



davethomaspilot
13th March 2017, 22:53
I want to have a QLineEntry that will acccept 8-63, printable, hex characters (wpa password).

A QRegExpValidator for this could look like this:


QRegExp rx("[ -~]{8,63}");
ui->hotSpot_PSK->setValidator(new QRegExpValidator(rx,this));

I can verify that


ui->hotSpot_PSK->hasAcceptableInput())

is true and false when expected. But, this doesn't seem to have any affect on the user can be entered. For example, the LineEntry "closes" when enter is pressed, even if less than 8 characters are entered.

Do I need to have code that tests for hasAcceptableInpu() in all the slots that would allow invalid text to be entered, including the buttons that do accept(), like OK and Apply in the typical button box?

If so, what does the validator really do for me (versus just a QRegExp test on the text()?

Thanks!

davethomaspilot
14th March 2017, 12:55
When I subclass QRegExpValidator and provide an inplementation of validate, things work like I expected. For example, it's not possible to delete characters to make the text less than 8 characters long.

I figured this is how it would work without needing to implement validate. If the RegExp didn't match, it would return Invalid, if it matched, Acceptable.

Am I missing something? Is it really necessary to subclass and inplement the validate method versus just defining the RegExp?

d_stranz
15th March 2017, 00:54
I don't see anything wrong with what you've done, except that QLineEdit does not take ownership of the validator, so setting it the way you do causes a (small) memory leak. You should not have to subclass QRegExpValidator.

You should not be getting the editingFinished() signal from the line edit if the widget loses focus (e.g. Enter is pressed) while the input is invalid. Is that what you observe?

Does the same thing happen if you use QRegularExpressionValidator instead?