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. [Solved] QRegExp and QValidator don't work as I need to
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved General and Desktop
9 Posts 3 Posters 2.3k 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.
  • R Offline
    R Offline
    roseicollis
    wrote on last edited by roseicollis
    #1

    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.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

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

      should do

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

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

      1 Reply Last reply
      0
      • R Offline
        R Offline
        roseicollis
        wrote on last edited by
        #3

        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?

        1 Reply Last reply
        0
        • K Offline
          K Offline
          koahnig
          wrote on last edited by
          #4

          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

          Vote the answer(s) that helped you to solve your issue(s)

          1 Reply Last reply
          0
          • R Offline
            R Offline
            roseicollis
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • K Offline
              K Offline
              koahnig
              wrote on last edited by
              #6

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

              Vote the answer(s) that helped you to solve your issue(s)

              1 Reply Last reply
              0
              • K Offline
                K Offline
                koahnig
                wrote on last edited by
                #7

                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.

                Vote the answer(s) that helped you to solve your issue(s)

                1 Reply Last reply
                0
                • R Offline
                  R Offline
                  roseicollis
                  wrote on last edited by
                  #8

                  [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 ...

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    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.

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

                    1 Reply Last reply
                    0

                    • Login

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