Using QValidator with QLineEdit's input mask

  • 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":
    Sets the QLineEdit's validation mask. Validators can be used instead of, or in conjunction with masks; see setValidator().

    Does anyone know the trick to get it to work?

    Thanks in advance.

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

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

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

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

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

    The online documentation for "QLineEdit": 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]

  • 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": (without input mask), but it seems too complicated :-)

  • 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":

    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.

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

  • 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 = 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.

Log in to reply

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