Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QLineEdit using a QRegExpValidator doesn't take effect.
QtWS25 Last Chance

QLineEdit using a QRegExpValidator doesn't take effect.

Scheduled Pinned Locked Moved Solved General and Desktop
qlineeditqregexpvalidatoqregexp
5 Posts 3 Posters 2.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Cysis145
    wrote on 2 Nov 2016, 21:52 last edited by SGaist 11 Mar 2016, 21:02
    #1

    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);
        ui->LE_ObjectName->setValidator(v);
    

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

    Thanks!

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 2 Nov 2016, 22:09 last edited by
      #2

      Hi,

      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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      C 1 Reply Last reply 2 Nov 2016, 23:03
      1
      • S SGaist
        2 Nov 2016, 22:09

        Hi,

        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

        C Offline
        C Offline
        Cysis145
        wrote on 2 Nov 2016, 23:03 last edited by Cysis145 11 Feb 2016, 23:05
        #3

        @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

        Thanks

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Cysis145
          wrote on 2 Nov 2016, 23:13 last edited by
          #4

          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]+");
          
          1 Reply Last reply
          0
          • V Offline
            V Offline
            VRonin
            wrote on 2 Nov 2016, 23:17 last edited by VRonin 11 Feb 2016, 23:18
            #5
            QRegularExpression rx("[A-Za-z0-9]*");
                QRegularExpressionalidator *v = new QRegularExpressionValidator(rx, this);
                ui->LE_ObjectName->setValidator(v);
            

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply
            3

            5/5

            2 Nov 2016, 23:17

            • Login

            • Login or register to search.
            5 out of 5
            • First post
              5/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved