PDA

View Full Version : How to call QValidator derived object



bruccutler
23rd March 2007, 16:54
I have a QLineEdit field which I need to have the user either enter 0 characters or 10 characters and nothing in between. I have written a QValidator class that returns invalid if anything other than 0 or 10 characters are entered. It also returns intermediate other times.

How do I prevent the user from moving out of the QLineEdit field until the field is correct?

Here's my code snippets:
(validate routine from QValidator derived class)

QValidator::State ValidatorWepKey::validate(QString &input, int &pos) const
{
if (input.length() == 0)
{
return Acceptable;
}
else if (input.length() < m_minLength)
{
return Intermediate;
}
else if (input.length() == m_minLength)
{
return Acceptable;
}
else
{
return Invalid;
}
}
Main code:

QLineEdit m_pKey1 = new QLineEdit();
m_pKey1->setValidator(new ValidatorWepKey(10, this)); // should prevent

When do I call the ValidatorWepKey.validate()? Is that when I get the signal that the user is attempting to exit the field? How do I keep them in the field until it is correct?

- BRC

jpn
23rd March 2007, 17:03
Using an input mask (http://doc.trolltech.com/4.2/qlineedit.html#inputMask-prop) might simplify your task a bit. Although forcing the focus to the line edit until a correct input has been entered might not be a good idea. Consider disabling the ways for proceeding or something like that instead.

bruccutler
23rd March 2007, 17:07
I did have a slot that responded when they entered incorrect information and set the focus back to the line edit, but it wasn't keeping them there. I'll go back to that approach of not letting them change the focus until the entry is correct.

So, can a mask enforce a minimum length?
- Bruce