PDA

View Full Version : Mixing QRegExpValidator and input mask and QLineEdit



abernat
18th February 2009, 00:13
I've set up a validator on a QLineEdit for entering a time interval:


QRegExp exp("\\d\\d:[0-5]\\d:[0-5]\\d");
lineEdit_->setValidator(new QRegExpValidator(exp, this));

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:


lineEdit_->setInputMask("99:99:99");

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.

wysota
18th February 2009, 01:04
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:

QRegExp exp("[ \\d]{2}:[0-5 ][ \\d]:[0-5 ][ \\d]");

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:

QRegExp exp("[ \\d]{2}(:[0-5 ][ \\d]){2}");

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.