⚠️ Forum Maintenance: Feb 6th, 8am - 14pm (UTC+2)

QLineEdit using a QRegExpValidator doesn't take effect.

  • Hi guys i'm trying to get my LineEdit to only take in letters and numbers. I've set up a QRegExpValidator and applied it to the lineedit however it doesn't appear to be working

    heres my code:

        QRegExp rx("[A-Za-z0-9]");
        QRegExpValidator *v = new QRegExpValidator(rx, this);

    Even with this the LineEdit just takes in any character. Not really sure what im doing wrong here


  • Lifetime Qt Champion


    Your current regex only allows one char at a time in the QLineEdit.

    Note that if you are using Qt 5,, you should move to QRegularExpression, its the replacement of the deprecated QRegExp

  • @SGaist I am using QT5 but after looking into QRegularExpression docs i dont see how i can apply it in this scenario.

    Also what do you mean by only allows one char at a time?

    I'm trying to get my LineEdit to accept only number and letter characters but without limiting the total number of characters in the LineEdit. At the moment it's taking in all the characters which i don't want (so no [,.#+- etc. symbols) Please Expand


  • Ah never mind i figured it out!

    Thanks for all your help, all i needed to do was change the QRegExp line to:

     QRegExp rx("[A-Za-z0-9]+");

  • QRegularExpression rx("[A-Za-z0-9]*");
        QRegularExpressionalidator *v = new QRegularExpressionValidator(rx, this);