[Solved] QRegExp and QValidator don't work as I need to



  • Hi all!

    I'm making a gui app which contains some lineedits to let the user introduce some information. For that I set some QRegExp and QValidator and it works fine except for 1 case that I hope that you can help me :)

    Case: a Lineedit that has to accept 3 letters which are ABC and max 5 numbers. For example... valid inputs: ABC12345 / ABC00000 / ABC1234 / ABC89865.... invalid inputs: ABC 1234 / AAA12345 / AB / ABC / A / ...
    I have this code but it acepts too A / AB / ABC/... and I want to force the user to write ABC and at least 2 numbers

    @QRegExp rx3("[A][B][C]\d{5}");
    QValidator *validator3 = new QRegExpValidator(rx3, this);
    lineedit_case1->setValidator(validator3); @

    So... any Idea of how can I do this?

    Thank you so much.


  • Lifetime Qt Champion

    Hi,

    @[ABC]{3}\d{1,5}$@

    should do

    If you're on Qt 5 you should rather use QRegularExpression



  • Hi Sgaist,

    Thanks for your help. I'm on Qt 4.8.5. With your code I have more problems. I can still put A or AB or ABC and its acepted as well... AND I can't put numbers after it O.o

    What i do is in the constructor of the class:

    @QRegExp rx3("[ABC]{3}\d{1,5}$");
    QValidator *validator3 = new QRegExpValidator(rx3, this);
    lineEdit>setValidator(validator3);
    @

    Should I do something else?


  • Moderators

    I think this one might work better:
    @
    QRegExp rx3("ABC\d{2,5}$");
    @
    This should accept only
    @
    ABC00
    ABC000
    ABC0000
    ABC00000
    @
    The letters are accepted only in sequence "ABC" no mutations are allowed. '0' symbolizes any digit in the examples.

    I you like to check your regular expressions, I found the "Regular expression example":http://doc.qt.io/qt-5/qtwidgets-tools-regexp-example.html quite helpful. AFAIK it should be already avaialble in Qt 4.8



  • Hi koahnig,

    Thanks for your example but it works the same way as mine in the first post :( . I think that the problem maybe is in the way I use it because I set it on the constructor of the class and then when user press a button of the screen ("Save data and quit") I just save it into an xml and then quit the app. My intention is that if the user has written ABC000 for example, the program saves and exit BUT if the user writes AB it does nothing and now it always save and exit no matter if you write ABC0000 or ABC or AB or A


  • Moderators

    Stupid question: Do you set the validator for the correct field?


  • Moderators

    Most likely the behaviour stems from the "reimplemented validate method":http://doc.qt.io/qt-4.8/qregexpvalidator.html#validate
    However, I do not know why the lineedit is exiting with an intermediate state.



  • [quote author="koahnig" date="1425390883"]Stupid question: Do you set the validator for the correct field?
    [/quote]

    Yes, hehe

    [quote author="koahnig" date="1425391643"]Most likely the behaviour stems from the "reimplemented validate method":http://doc.qt.io/qt-4.8/qregexpvalidator.html#validate
    However, I do not know why the lineedit is exiting with an intermediate state. [/quote]
    Either do I ...


  • Lifetime Qt Champion

    Sorry, I misunderstood that A0 wasn't a valid input.

    so
    @"ABC\d{1,5}$"@

    Should do the job for the editing part.

    As for saving, you can use the change signal and in the connected slot, check if the state is valid. If not, disable the save button.


Log in to reply
 

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