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 (10th 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 (10th 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 16:28. Reason: fixed a typo
whitefurrows (10th 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 (10th 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 16: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 (10th June 2006)
Hi,
that's work fine. Thank you for your great help.
Greetings,
Whitefurrows
Hi,
sorry i have trouble with my own QDoubleValidator, i set a Range 5-25 but i can't type 15 in to the QLineEdit:
myLineEdit->setValidator(new MyDoubleValidator( 5, 25, 2, myLineEdit));
Can anybody help me?
Greetings,
Whitefurrows
Qt Code:
{ public: { const double b = bottom(); const double t = top(); const int d = decimals(); if (input.contains(' ')) return Invalid; return Invalid; if (empty.exactMatch(input)) return Intermediate; bool ok = false; double entered = input.toDouble(&ok); if (!ok) return Invalid; int nume = input.count('e', Qt::CaseInsensitive); int i; if (input.contains(',')) i = input.indexOf(','); else i = input.indexOf('.'); if (i >= 0 && nume == 0) { i++; int j = i; while(input[j].isDigit()) j++; if (j - i > d) return Invalid; } if (entered < b || entered > t) return Invalid; return Acceptable; } };To copy to clipboard, switch view to plain text mode
The problem is here:If the allowed range is 5--25, then "1" should be an intermediate value, not invalid one.Qt Code:
if (entered < b || entered > t) return Invalid;To copy to clipboard, switch view to plain text mode
It should be probably:Qt Code:
if( entered < b ) { return Intermediate; } else if( entered > t ) { return Invalid; }To copy to clipboard, switch view to plain text mode
whitefurrows (10th June 2006)
Hi,
sorry i don't have get a notification. That's right, but the user can put wrong values to the QLineEdit.
What can i do?
Greetings,
Whitefurrows
The problem is that line edit can loose focus when it contains an intermediate value. IMO you should ask the Trolls whether this is a correct behavior.Originally Posted by whitefurrows
whitefurrows (10th June 2006)
Hi,
thank's to you. You know a way how can i set focus to a QLineEdit as long as contains an intermediate value?
Greetings
Whitefurrows
There are many ways to do that. You could override line edit's focusOutEvent(), use an event filter, or install an event filter even on the application. In all cases, you'll just have to ask the validator for it's state and then set the focus back on the line edit when appropriate.
J-P Nurmi
whitefurrows (10th June 2006)
Hi,
can you help me to do that, please?
I try that:
Qt Code:
protected:To copy to clipboard, switch view to plain text mode
Qt Code:
int i =0; while (ob!=lineEditList[i] && i<lineEditList.size()) i++; if(LineEdit Validator state == intermediate) //How can i do that? leList[i]->setEditFocus(true); }To copy to clipboard, switch view to plain text mode
Thanks in advance,
Whitefurrows
The focus out event could look for example something like this:
Qt Code:
{ // check validator state int pos = 0; { // keep focus if intermediate setFocus(); } else { // otherwise call base class implementation and let it // handle focus out actions (so cursor stops blinking etc.) } }To copy to clipboard, switch view to plain text mode
If you have difficulties in subclassing QLineEdit, I suggest you to switch back to your favourite C++ book and learn the secrects of inheritance..
J-P Nurmi
whitefurrows (10th June 2006)
Hi,
the problem is i can't subclassing QLineEdit. I use the designer and the designer use the QLineEdit and not MyLineEdit.
Sorry my stupid question i'm a beginner and learn. Please help me one more time.
Greetings,
Whitefurrows
Bookmarks