how to check the range of data entered in Qlineedit
-
I have a screen in which i will display the numbers pressed(Eg : similar to a calculator) in a Qlineedit . But i want to check that the number entered in Qlineedit should be from the range of 4400 to 5000 and it should accept any decimal value from this range (for eg it should accept 4500.5 and it should not accept 1111 or any number that starts with 1,2,3,6,7,8,9,0) how can we do this.
-
You can use QLineEdit::setValidator to achieve this, together with QDoubleValidator or QIntValidator.
Or, alternatively, use QSpinBox/ QDoubleSpinbox.
-
@sierdzio i dont want to use spin box . and i have tried using
QDoubleValidator *validator = new QDoubleValidator(4400.0, 5000.0,3,ui->TxtInput);
validator->setNotation(QDoubleValidator::StandardNotation);
ui->TxtInput->setValidator(validator);and i want the line edit to only accept the data from range 4400 - 5000) . How to do it.
-
After user edits the line edit, check acceptableInput property. If it is not acceptable, you can reset the value.
My guess is that
1111
is seen as an intermediate value - because the user can still change it to4911.11
which is acceptable. -
@ManiRon said in how to check the range of data entered in Qlineedit:
how to make it to accept only between 4500 - 5000
In the way @sierdzio explained: call acceptableInput() when user finishes typing (pressing enter or some button, moving focus to another widget), if it returns false reset the value in the editor and ask user to input valid value.
The problem is that the final value is available when the user finished entering it, but how can the editor know when the user is finished? For example you're entering 45 - this is not acceptable value, right? But if you continue and enter 4501 it is valid, right?"i checked the acceptable input and it returns 0" - which is false, means not acceptable...
-
@ManiRon
look at that, I'm can do magic after all, a little bit at least ;-)int main(int argc, char *argv[]) { QApplication a(argc, argv); QLineEdit *le = new QLineEdit(); le->setInputMask(QString("9999")); le->setText("4500"); QRegularExpression re("(4[5-8][0-9]{2}|49[0-8][0-9]|499[0-9]|5000)"); QRegularExpressionValidator *validator = new QRegularExpressionValidator(re); le->setValidator(validator); le->show(); return a.exec(); }
-
@J.Hilk
I do not mean to be disrespectful, but IMHO showing someone how to validate an intended input numeric range via a (complex) regular expression such as yours which tries to capture the acceptable input as a string limited to certain digit patterns is not a good idea. It is difficult to write/maintain, and is far too brittle for possible future changes in the range accepted (which may well not be doable by reg exs at all). It should be done via a function which converts the characters to a number and validates that. -
@JonB hey, that's quite literally the 3rd time I used a regular expression and like both times before I used the
Internet
to get the actual expression:
http://gamon.webfactional.com/regexnumericrangegenerator/This is all magic hand waving for me.
And I agree with your point, a custom/sub classed QLineEdit is the better way to do it, in my opinion at least.
-
@J.Hilk
Huh? You are a "Moderator" here, so I expect you to be god-like in your knowledge/experience --- how can you possibly only have used a reg ex 3 times, I have been using them for more than a quarter of a century! :)Your reg ex is (probably) perfectly correct for capturing the precise requirement expressed by the OP, I am not saying it isn't. But it's like pushing a square peg into a round hole here: it fits but it's wobbly, there are gaps round the edges, and once it's forced in it will be difficult to get it back out :) The OP should use a round peg instead!
-
@JonB said in how to check the range of data entered in Qlineedit:
@J.Hilk
Huh? You are a "Moderator" here, so I expect you to be god-like in your knowledge/experience ---I'm new :P
how can you possibly only have used a reg ex 3 times, I have been using them for more than a quarter of a century! :)
I do use it more often, but its a fixed expression I haven't touch since the beginning, I use it for Date & Time edits -> 2 RegExp and the preciously posted one as the 3rd ;)
Your reg ex is (probably) perfectly correct for capturing the precise requirement expressed by the OP, I am not saying it isn't.
It actually is not, you can't enter 5000, only 4500 to 4999. Because 0 is not allowed at the 2nd place -> you can't enter 5 at the first place.
I should have tested more before posting....
-
@J.Hilk
That's why I said it would be brittle, and there are likely to be certain ranges for which no pattern is suitable (haven't thought it through, just assuming so), or at least difficult.It actually is not, you can't enter 5000, only 4500 to 4999. Because 0 is not allowed at the 2nd place -> you can't enter 5 at the first place.
No, 5000 is OK, because you are using
|
and you have|5000
as an explicit allowance.I now see your link is to somewhere where they generate a numeric range reg ex for you. IMHO that should come with a health warning! BTW I randomly typed in range
123456
to7980678
and it gives:(12345[6-9]|1234[6-9][0-9]|123[5-9][0-9]{2}|12[4-9][0-9]{3}|1[3-9][0-9]{4}|[2-9][0-9]{5}|[1-6][0-9]{6}|7[0-8][0-9]{5}|79[0-7][0-9]{4}|7980[0-5][0-9]{2}|79806[0-6][0-9]|798067[0-8])
which again may well be correct but is hardly nice reading, is it? :)
Also btw, assuming you use something like Qt Creator for your coding, haven't you used reg exs there from time to time when searching your code? That counts as reg ex experience too!