QValidator manual validation.

  • Hi,
    I got problem with manual validation of handled text.

    I got situation where I take validator for QLineEdit::validator() and I want to check if my current text is passing validators conditions.

    For e.g.
    1)I set validator

    this->edit1->setValidator(new QRegExpValidator(QRegExp("[A-Za-z]{5}"),this));//no more than 5 letter character

    2)When I try to access this validator from the pointer to QlineEdit and execute QValidator::validate(text, pos(0))

    1. "a" i get QValidator::Intermediate (should be QValidator::Acceptable)
    2. "aa" I get QValidator::Intermediate (should be QValidator::Acceptable)
    3. "aaa" I get QValidator::Intermediate (should be QValidator::Acceptable)
    4. "aaaa" I get QValidator::Intermediate (should be QValidator::Acceptable)
    5. "aaaaa" I get QValidator::Invalid (should be QValidator::Acceptable)

    Do I get something wrong?
    How to use that validation?
    If I not able to validate handled text by this method could anyone say how can I do that?

    Best regards,

  • Moderators

    {5} means "exactly 5 times" so the fist 4 strings will be partial matches (intermediate state).
    to have "no more than 5" use {0,5}.

    As for the last case - it returns Acceptableto me as expected. Can you show the code that calls validate()? Also what is text and pos(0)?

    As a side note - you should switch to QRegularExpressionValidator if possible. It uses the recommended, Perl compatible, replacement of QRegExp - QRegularExpression.

  • Hi,
    You are completely right. I was wrong with assumption that {5} means no more than five.

    Text is some text. pos(0) means that I put there 0 value, but do You maybe know whats that parameter really do?

    Best regards,

  • Moderators

    @Kaluss said:

    but do You maybe know whats that parameter really do?

    It's suppose to be "cursor position", but in case of regexp validator it's really a useless parameter to satisfy the interface of QValidator. It gets set to the length of the string if the regexp validator doesn't find a match.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.