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)
{
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;
}
}
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;
}
}
To copy to clipboard, switch view to plain text mode
Main code:
m_pKey1->setValidator(new ValidatorWepKey(10, this)); // should prevent
QLineEdit m_pKey1 = new QLineEdit();
m_pKey1->setValidator(new ValidatorWepKey(10, this)); // should prevent
To copy to clipboard, switch view to plain text mode
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
Bookmarks