I want to accept a new username from the user in my application. I want the username string to contain only A-Z
or a-z
or 0-9
, and have a maxLength of 8. So I want to validate the input from QLineEdit
. I went through the documentation but I am messed up with validators. How I can validate my QLineEdit
for this purpose?
Asked
Active
Viewed 1.2k times
3

Jason Plank
- 2,336
- 5
- 31
- 40

Surjya Narayana Padhi
- 7,741
- 25
- 81
- 130
1 Answers
7
You can use setInputMask to specify the validation input mask, in your case you can use "N" or "n' to permit only characters in A-Z, a-z and 0-9 range. something like this:
lineEdit->setInputMask("nnnnnnnn;_"); // or NNNNNNNN;_
lineEdit->setCursorPosition(0);
You also can set QValidator
instance to your lineEdit
, via set setValidator
. This sets the lineEdit
to only accept input that the validator, will accept. This will work in conjunction with edit masks
If you only need to restrict the max permitted length of the line edit: use setMaxLength
lineEdit->setMaxLength(8);
hope this helps, regards

AAEM
- 1,837
- 2
- 18
- 26

serge_gubenko
- 20,186
- 2
- 61
- 64
-
Thanks a lot for the reply. Actually with inputMask it restricts the other other keys to detect. So I want it to accept and echo any key pressed and after user is done entering the username, I want to check the validation and can show login success or fail on my criteria. Could you please give me some code using QRegExValidator? It will be helpful to me. – Surjya Narayana Padhi Mar 07 '11 at 01:53
-
2Example usage of QRegExpValidator is in QRegExpValidator documentation in Qt docs (detailed description) – Kamil Klimek Mar 07 '11 at 12:34