PDA

View Full Version : How many validators can be applied to a single QLineEdit?



awhite1159
12th July 2008, 13:57
I have a QLineEdit in a dialog box I am creating that allows entering an IP address. I have used a QRegExp validator to ensure that it takes the form:

"[0-2][0-9][0-9][.][0-2][0-9][0-9][.][0-2][0-9][0-9][.][0-2][0-9][0-9]"

(I know I need some improvement with regular expressions...:D)

This works ok but I still need to range check so each of the four values are within 0-255.

Can multiple validators be applied to the same widget?

Or, am I going about this all wrong?

I was thinking it might be better to break down the QLineEdit into four separate ones, use the range checking of a QIntValidator and then convert to one single QString after it is valid?

jacek
12th July 2008, 14:08
This works ok but I still need to range check so each of the four values are within 0-255.
You can achieve this with a regular expression.


Can multiple validators be applied to the same widget?
No, but you can create a CompositeValidator class that will aggregate several validators.

awhite1159
12th July 2008, 14:29
Thanks Jacek.

As I said, I need to improve my regular expressions knowledge. Time to pull out my O'Reilly AWK reference and read up on regular expressions.

jacek
12th July 2008, 14:35
Time to pull out my O'Reilly AWK reference and read up on regular expressions.
See this one: http://regex.info/

awhite1159
15th July 2008, 01:18
Got it:
QRegExp rx("(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)");

(All on a single line.)