PDA

View Full Version : mask line edit



steg90
3rd July 2007, 09:42
Hi,

I was wondering if it is possible to have a line edit which only allows you to enter only maximum of 4 hex digits and in the range 0 - FFFF. I know this can be done with a QValidator, but this seems to be only after you have entered the data in the line edit. For instance, I could type in FEFEFEFEFE and it would only be when I press enter or something that I can validate the data. Do I need to override the keyPressEvent and subclass QLineEdit or just simply use setInputMask?

Regards,
Steve

jpn
3rd July 2007, 09:51
Hi, QLineEdit::inputMask should be sufficient:


H Hexadecimal character required. A-F, a-f, 0-9.
h Hexadecimal character permitted but not required.

So try "Hhhh;"

jpn
3rd July 2007, 10:16
Hmm, actually there is one problem with input masks (at least with the aforementioned one). User is able to enter for example "F FF" (notice the space in between). So QRegExpValidator might actually be a better choice. Try something like this:


QRegExp rx("(0(x|X))?(\\d|[a-f]|[A-F]){1,4}");
QValidator* validator = new QRegExpValidator(rx, lineEdit);
lineEdit->setValidator(validator);

wysota
3rd July 2007, 10:21
You can use QwwLineEdit from wwWidgets which allows entering a regexp for a validator as a property of the widget. This way you can save a few lines of code ;)

steg90
3rd July 2007, 10:27
Many thanks.

JPN - that works a treat ;)