Results 1 to 2 of 2

Thread: Mixing QRegExpValidator and input mask and QLineEdit

  1. #1
    Join Date
    Feb 2009
    Posts
    24
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Mixing QRegExpValidator and input mask and QLineEdit

    I've set up a validator on a QLineEdit for entering a time interval:

    Qt Code:
    1. QRegExp exp("\\d\\d:[0-5]\\d:[0-5]\\d");
    2. lineEdit_->setValidator(new QRegExpValidator(exp, this));
    To copy to clipboard, switch view to plain text mode 

    This works just fine. The problem is when I try to add an input mask. I want the colons to show up automatically. I tried:

    Qt Code:
    1. lineEdit_->setInputMask("99:99:99");
    To copy to clipboard, switch view to plain text mode 

    to no avail. It won't let me enter anything. Anyone have any insight as to the proper way to mix validators and masks?

    Also, if I call lineEdit_->setValidator(0), will it automatically delete the validator object that was previously being used or is there a memory leak if I don't delete it myself?

    Thanks.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Mixing QRegExpValidator and input mask and QLineEdit

    You have to make the digits optional and make it possible to input spaces because the "__:__:__" entry (where "_" is a blank space) has to be valid to satisfy the input mask.

    So the regexp should probably look like this:
    Qt Code:
    1. QRegExp exp("[ \\d]{2}:[0-5 ][ \\d]:[0-5 ][ \\d]");
    To copy to clipboard, switch view to plain text mode 

    In English: two characters where each is either a digit or a blank followed by a colon followed by a digit from 0 to 5 or a blank followed by a digit or blank followed by a colon followed by 0-5 or blank folowed by a digit or blank.
    Hmm... you can probably simplify it to:
    Qt Code:
    1. QRegExp exp("[ \\d]{2}(:[0-5 ][ \\d]){2}");
    To copy to clipboard, switch view to plain text mode 

    which says two (digits or blank) followed by two groups consisting of a colon followed by 0-5 or blank and digit or blank.

    And about the second question - the validator will be deleted if you make the line edit its parent.

Similar Threads

  1. QLineEdit and input mask
    By tpf80 in forum Qt Programming
    Replies: 7
    Last Post: 9th May 2014, 19:47
  2. Replies: 10
    Last Post: 12th February 2009, 07:23
  3. Pointer Question related to QLineEdit
    By ChrisReath in forum Qt Programming
    Replies: 1
    Last Post: 23rd May 2008, 15:13
  4. QValidator, regular expressions and QLineEdit
    By hvengel in forum Qt Programming
    Replies: 1
    Last Post: 8th August 2007, 01:25

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.