Results 1 to 6 of 6

Thread: what's wrong with my validator?

  1. #1
    Join Date
    Jan 2006
    Location
    Munich, Germany.
    Posts
    111
    Thanks
    29
    Thanked 3 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default what's wrong with my validator?

    Qt4

    I've got a QDoubleValidator on a QLineEdit as shown below

    Qt Code:
    1. LineEdit = new QLineEdit;
    2. LineEdit->setValidator(new QDoubleValidator(_MIN_BIAS_AMP, _MAX_BIAS_AMP, 2, LineEdit));//bottom, top, decimals, parent
    To copy to clipboard, switch view to plain text mode 

    where _MIN_... and _MAX are const doubles defined somewhere else.

    Problem: The line edits allow input which is larger (or smaller) than the _MAX (_MIN). They (correctly) DON'T send the signal returnPressed() when the return key is pressed.
    Example:
    with
    setValidator(new QDoubleValidator(-100.0, 100.0, 2, LineEdit));
    I can type "1000000" or "-99999999" into the LineEdit. I thought that the Validator would not allow that.

    I'm I missing a setting?

    thanks
    K

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: what's wrong with my validator?

    Quote Originally Posted by TheKedge
    with
    setValidator(new QDoubleValidator(-100.0, 100.0, 2, LineEdit));
    I can type "1000000" or "-99999999" into the LineEdit. I thought that the Validator would not allow that.
    AFAIR QDoubleValidator permits the "scientific" notation, so you can write 1000000e-4 or -99999999e-5.

  3. #3
    Join Date
    Jan 2006
    Location
    Munich, Germany.
    Posts
    111
    Thanks
    29
    Thanked 3 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: what's wrong with my validator?

    Ahh, (the penny drops). of course, I can type an "e" into the validator.

    Ok, but now I have another problem: how can I let the user know that he/she has entered a value that won't be used. cos, if I enter 10000 and the limit is 100, nothing happens - "10000" stays in the LineEdit. The user can't know if the value has been accepted! The LineEdit won't emit a returnPressed() or a editingFinished().

    thanks
    K

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: what's wrong with my validator?

    You could try connect to QLineEdit::textChanged(), check validator's state and provide some feedback to user (like changing QLineEdit background or adding some icon next to it).

  5. #5
    Join Date
    Jan 2006
    Location
    Mountain View, CA
    Posts
    279
    Thanked 42 Times in 37 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: what's wrong with my validator?

    Quote Originally Posted by jacek
    AFAIR QDoubleValidator permits the "scientific" notation, so you can write 1000000e-4 or -99999999e-5.
    Jacek is correct. If you don't need scientific notation, you can better achieve what you want with a regular expression validator:

    Qt Code:
    1. setValidator(new QRegExpValidator(QRegExp("[-+]?[0-9]{0,3}(\\.[0-9]{0,2})"), LineEdit));
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany.
    Posts
    111
    Thanks
    29
    Thanked 3 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: what's wrong with my validator?

    Yes, of course, RegEx.
    just one thing though: at compile time I don't know what my min, max are.
    Still, no way around that without the work - I'll have convert my max to a string parse it and build my RegEx. Or validate by hand as suggested.

    Thanks guys,
    K

Similar Threads

  1. How to set Validator for individual cell in QTable
    By raghvendramisra in forum Qt Programming
    Replies: 3
    Last Post: 13th September 2008, 15:51
  2. Replies: 2
    Last Post: 2nd June 2008, 08:45
  3. IPAddress Validator RegExp
    By user_mail07 in forum Qt Programming
    Replies: 0
    Last Post: 3rd April 2008, 02:36
  4. Validator for QDoubleSpinBox
    By ehamberg in forum Qt Programming
    Replies: 3
    Last Post: 15th March 2008, 11:37
  5. Help please - what am I doing wrong?
    By Jimmy2775 in forum Qt Programming
    Replies: 6
    Last Post: 6th March 2006, 22:06

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.