PDA

View Full Version : what's wrong with my validator?



TheKedge
26th January 2006, 17:09
Qt4

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


LineEdit = new QLineEdit;
LineEdit->setValidator(new QDoubleValidator(_MIN_BIAS_AMP, _MAX_BIAS_AMP, 2, LineEdit));//bottom, top, decimals, parent

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

jacek
26th January 2006, 17:54
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.

TheKedge
26th January 2006, 18:13
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

jacek
26th January 2006, 18:30
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).

Chicken Blood Machine
26th January 2006, 18:33
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:



setValidator(new QRegExpValidator(QRegExp("[-+]?[0-9]{0,3}(\\.[0-9]{0,2})"), LineEdit));

TheKedge
26th January 2006, 22:41
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