Hi,
how can i set a Validator to a QLineEdit that accept only values between 1.00 and 16.00 ?
Thanks in advance,
Whitefurrows
Hi,
how can i set a Validator to a QLineEdit that accept only values between 1.00 and 16.00 ?
Thanks in advance,
Whitefurrows
QSpinBox (or QDoubleSpinBox) would have the required behaviour, out of the box...![]()
J-P Nurmi
whitefurrows (11th June 2006)
I need a clear input field like a QLineEdit. I can't use a QSpinBox or QDoubleSpinBox because have a button on the right side.
One more tip ?
Then use QDoubleValidator or QRegExpValidator (regexp should be something like "(?:1[0-5]|[1-9])(?:[.,]\\d\\d?)?|16(?:[.,]00?)?").
whitefurrows (11th June 2006)
Hi,
that's great your regexp work fine. I read the doc for QRegExp but i dont know what you do. Can you describe how your regexp work and i understand that. The problem is i have many other ranges to set e.g. 0-50, 15-32, 65-105 and i can't change your regexp.
How can i set a QDoubleValidator its's easey, but my code don't work. I can set values out of range. Why?
Qt Code:
To copy to clipboard, switch view to plain text mode
Greetings,
Whitefurrows
In such case, IMO, it would be better if you create your own validator class, because regexps aren't very readable.Originally Posted by whitefurrows
Anyway, suppose you want a regexp for 65-105 range. Valid numbers are:
65, 66, 67, 68, 69, 70, ..., 79, 80, ..., 89, 90, ..., 99, 100, 101, 102, 103, 104, 105
There are three possibilities:
- 6 and one of {5, 6, 7, 8, 9} -> 6[5-9]
- one of {7, 8, 9} and a digit -> [7-9]\\d
- 10 and one of {0, 1, 2, 3, 4, 5} -> 10[0-5]
and you just have to join them: 6[5-9]|[7-9]\\d|10[0-5]
Because you can enter 1500.00e-2, which is a valid number.Originally Posted by whitefurrows
Last edited by jacek; 21st May 2006 at 17:28. Reason: fixed a typo
whitefurrows (11th June 2006)
OK i have understand how work the regexp, thank you.
I think really it's better to write my own validator class.
Qt Code:
{ public: { if ( input.isEmpty() || input == "." ) return Intermediate; //I'm not sure what can i do here return Invalid; } return Acceptable; } };To copy to clipboard, switch view to plain text mode
Can you help me please?
Take a look at QDoubleValidator::validate() implementation (it's in %QTDIR%/src/gui/widgets/qvalidator.cpp). All you have to do is to remove the part that handles the exponent.
whitefurrows (11th June 2006)
I don't want change orginal files. I try this:
Qt Code:
{ public: {} { if (input.contains(' ')) return Invalid; return Invalid; if (empty.exactMatch(input)) return Intermediate; bool ok = true; double entered = input.toDouble(&ok); int nume = input.count('e', Qt::CaseInsensitive); if (!ok) { // explicit exponent regexp int eePos = expexpexp.indexIn(input); if (eePos > 0 && nume == 1) { entered = mantissa.toDouble(&ok); if (!ok) return Invalid; if (expexpexp.cap(1).isEmpty()) return Intermediate; } else if (eePos == 0) { return Intermediate; } else { return Invalid; } } int i = input.indexOf('.'); if (i >= 0 && nume == 0) { // has decimal point (but no E), now count digits after that i++; int j = i; while(input[j].isDigit()) j++; if (j - i > d) return Intermediate; } if (entered < b || entered > t) return Intermediate; return Acceptable; } };To copy to clipboard, switch view to plain text mode
but i can't compile!
Last edited by whitefurrows; 21st May 2006 at 17:18.
You're on the right track.Originally Posted by whitefurrows
It should be:
Qt Code:
{ public: {} { // we don't have the access to private members of QDoubleValidator, // so we must simulate them: const double b = bottom(); const double t = top(); const int d = decimals(); if (input.contains(' ')) return Invalid; ... } };To copy to clipboard, switch view to plain text mode
whitefurrows (11th June 2006)
Hi,
that's work fine. Thank you for your great help.
Greetings,
Whitefurrows
Bookmarks