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. Using QValidator with QLineEdit's input mask
QtWS25 Last Chance

Using QValidator with QLineEdit's input mask

Scheduled Pinned Locked Moved General and Desktop
10 Posts 3 Posters 19.1k 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.
  • K Offline
    K Offline
    koahnig
    wrote on last edited by
    #1

    I have created with the Designer an ui-file with line edits. For the line edits I have defined input masks. In addition I have defined later when using the ui some validators (QIntValidator). When I start my applcaition I cannot input anything in the line edits.

    The problem is that the input masks and the validators do apparently not cooperate. First I thought I have done something stupid, because both are some checking/validating my input. However, by consulting the documentation I have "found":http://doc.qt.nokia.com/4.7/qlineedit.html#inputMask-prop
    [quote]
    Sets the QLineEdit's validation mask. Validators can be used instead of, or in conjunction with masks; see setValidator().
    [/quote]

    Does anyone know the trick to get it to work?

    Thanks in advance.

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

    1 Reply Last reply
    0
    • L Offline
      L Offline
      loladiro
      wrote on last edited by
      #2

      QValidator only allows valid values to be entered into the QLineEdit. Can you show us your QValidator code?

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

        This is the code section:
        @
        //ui.leInteger->setInputMask ( "00000" );
        QIntValidator *qiv = new QIntValidator ( 0, 65535, this );
        ui.leInteger->setValidator ( qiv );
        @

        When removing the input mask in designer the code segment above code does work as it should.
        When using the input mask set in the code here, it has the same behaviour as with the input mask in the designer.

        Certainly I try to input only digits.

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

        1 Reply Last reply
        0
        • L Offline
          L Offline
          loladiro
          wrote on last edited by
          #4

          QIntValidator is sufficient. You do not need the input mask if you only want to input digits.

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

            Yes, I know. That is the reason thinking I am doing something stupid when having the problem.
            By consulting the documentation I have stumbled over the quote as in my first post.

            So either the documentation is wrong or I am missing the trick.

            Actually the other line edit is a bit more complicated. It is asking for an ip address. I had added the input mask, but I thought later, it would be nice to check immediately. There I have written my own validator. When this did not work, I have tried to make it work for the port first.

            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
              #6

              More out of curiosity. Is there someone knowing how to combine QValidator's and QLineEdit's functionalities?

              The online documentation for "QLineEdit":http://developer.qt.nokia.com/doc/qt-4.7/qlineedit.html describes under inputmask:
              [quote]Sets the QLineEdit's validation mask. Validators can be used instead of, or in conjunction with masks; see setValidator().[/quote]
              and a little further down:
              [quote]To get range control (e.g., for an IP address) use masks together with validators.[/quote]

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

              1 Reply Last reply
              0
              • C Offline
                C Offline
                cincirin
                wrote on last edited by
                #7

                Hi @koahnig,

                Have you found something relevant in this ? I am also interested in how to combine input mask with validators. For correct IP address I found "this":http://lists.trolltech.com/qt-interest/2004-05/thread00085-0.html (without input mask), but it seems too complicated :-)

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

                  Hi cincirin

                  initially I was putting aside the problem. Later on I have found that my problem came from the different properties you can set in the designer. I believe it was the initialization did not match the validator requirement. The example above was only simplified.
                  My advice is that if you really change/set the stuff already in the designer, make sure that matches, what you like to have. In my case it was a bit hard to find, since designer somewhat hides the details respectively you might overlook easily what you have done.

                  For ip address input I have an implementation based "on this":http://qt-project.org/forums/viewthread/5736/#34201

                  I "improved" it a bit, because I was not satisfied about the behavior. But it became even more complex and it is still not satisfactory. The QRegExp or regular expressions are certainly something to get used to.

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

                  1 Reply Last reply
                  0
                  • C Offline
                    C Offline
                    cincirin
                    wrote on last edited by
                    #9

                    Ok thanks. Actually I don't look for IP address validator. It was just a test case :-) What I looking for is to enter dimensions in feet/inches (i.e. 12' 4") with minim/maxim feet and inches. But I think by subclassing QValidator is approachable.

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

                      My start would be probably with the ip address example and simplify. QRegExp is quite powerful, but need to be "fluent" with it. The validator issue becomes tricky for cases like
                      _4'__6" = 4'6" = 4'6" ('' symbolizes a space)
                      For humans it is easy to recognize. For ip address it would be:
                      127.0.0.1 = 127.__0__0.__1 = 127.0.000.001 and whatever combinations are possible. My gut feeling is that it is manageable with QRegExp, but you need a real solid understanding. It was not worth for me at the end.

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

                      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