Why setValidator and setEchoMode on LineEdit erases the info? and HOW to override QValidator??
-
Hi,
I have a LineEdit for a password which has to be numeric and max. lenght is 8. Also it has to display asterisks or dots as I see it does with my code. My problem is that if I put the next code when I write the 9th digit which should not let you to, it ERASES all the lineedit and writtes it as the first one. O.o Why do I have this behaviour?
QRegExp rx212("[0-9]{0,8}"); QValidator *validator212 = new QRegExpValidator(rx212, this); // this both lines make it goes wrong le_Pwd->setValidator(validator212); // this line alone allows me to put 0-8 numbers, no more. This is what I want but mask with ****** le_Pwd->setEchoMode(QLineEdit::Password); // this line alone allows me to show dots instead of numbers to mask the pwd. with no limit
Thank you!
-
@roseicollis From what I can see in the documentation:
The length of the text can be constrained to maxLength(). The text can be arbitrarily constrained using a validator() or an inputMask(), or both. When switching between a validator and an input mask on the same line edit, it is best to clear the validator or input mask to prevent undefined behavior.
You can also set the maximum length via setMaxLength(int)
Edit: I may have read the documentation wrong. As it can be read 2 ways. However, I think using maxLength will prevent the user from typing a 9th character.
-
Hi,
If you set the validator to accept only the numarical input it shuld not accept any char.
Check this out, it may help you.TextField { // You can even use TextInput.
maximumLength: 8
inputMethodHints: Qt.ImhDigitsOnly // If your using QtVirtual Keyboard, this will help you to popup only numarical pad
validator: RegExpValidator{regExp: /^(([01]?[0-9]?[0-9]|2([0-4][0-9]|5[0-5])).){3}([01]?[0-9]?[0-9]|2([0-4][0-9]|5[0-5]))$/}
// Validate as you need
} -
@Pradeep-P-N said:
TextField { // You can even use TextInput.
maximumLength: 8
inputMethodHints: Qt.ImhDigitsOnly // If your using QtVirtual Keyboard, this will help you to popup only numarical pad
validator: RegExpValidator{regExp: /^(([01]?[0-9]?[0-9]|2([0-4][0-9]|5[0-5])).)([01]?[0-9]?[0-9]|2([0-4][0-9]|5[0-5]))$/}
// Validate as you need
}Hi, I was just seeing htat option in another post but.. isn't it for Qt 5+ ? I'm on Qt 4.8.5 and it didnt recognize it.
Another thing... where do you put it? in the middle of the code?? and how can I add it to the layout where it has to go? Before I was using: VLayout->addwidget(le_Pwd);Thanks!
-
I ran an example in both 4.8.6 and 5.4.1 and found that this bug has been resolved in the later.
So either update to a later version of Qt or catch input yourself before. (with input, don't just look at key presses, but also copy/paste and dead keys).
-
Mmmm... that was what I was afraid of... thas maybe it could be a bug... In which version was this bug fixed? where do you see this?
What u say about catch the input.. I already tried too with that:
in .h: class MyValidator2: public QValidator { public: MyValidator2(QObject* parent=nullptr): QValidator(parent) {} State validate(QString& input, int&) const override { if (input=="0" or input=="1" or input=="2" or input=="3" or input=="4" or input=="5" or input=="6" or input=="7" or input=="8" or input=="9") return QValidator::Acceptable; } }; in constructor .cpp: auto validator12 = new MyValidator2(parent); le_Pwd->setValidator(validator12); le_Pwd->setMaxLength(8); le_Pwd->setEchoMode(QLineEdit::Password);
But if i write 1 and then 2, the first one is erased and it only allows me to write one number.... and I don't know if its the bug or if I did wrong the validator ...
EDIT: I tried also with:
class MyValidator2: public QValidator { public: MyValidator2(QObject* parent=nullptr): QValidator(parent) {} State validate(QString& input, int&) const override { static const char alphanum[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz"; for (int i = 0; i < sizeof(alphanum) - 1; ++i) { if (input == &alphanum[i]) return QValidator::Invalid; else return QValidator::Acceptable; } return QValidator::Acceptable; } };
But either works...