How to make the text edit to allow for number only?? It should not allow any alphabets to enter on it..
How to make the text edit to allow for number only?? It should not allow any alphabets to enter on it..
see QValidator
More specifically QIntValidator.
==========================signature=============== ==================
S.O.L.I.D principles (use them!):
https://en.wikipedia.org/wiki/SOLID_...iented_design)
Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.
True, but I took a guess.![]()
==========================signature=============== ==================
S.O.L.I.D principles (use them!):
https://en.wikipedia.org/wiki/SOLID_...iented_design)
Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.
QIntValidator and QDoubleValidator are not good choice because they allow to input numbers that contains digits, +- signs, decimal point sign etc. You should use QRegExpValidator with regular expression [0-9]*
P.S.
Ooops. You write about number input not digit. QIntValidator and QDoubleValidator are good choice.
Last edited by Lesiok; 23rd March 2011 at 16:41.
How to make the text edit to allow for number only??-12.234 is still a number.QIntValidator and QDoubleValidator are not good .... numbers that contains digits, +- signs, decimal point sign etc.
Since he didn't provide any information about other restrictions all solutions suggested so far MIGHT be correct, but not necessarily.
==========================signature=============== ==================
S.O.L.I.D principles (use them!):
https://en.wikipedia.org/wiki/SOLID_...iented_design)
Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.
Guys, there is no need for any validators here. One needs to listen to the contentsChange() signal from QTextDocument, extract the characters added and use QString::toInt() or other appropriate variant to see if it is of the required class. Then the changes made need to be undone (in its simplest form by calling undo) if needed. If you intend to validate 100 pages of numbers using QIntValidator then, well... have a good time![]()
I'd subclass the text edit, call it numberEdit, and add it to my library of components. Then next time you want a similar function it's already done so you just grab the code
(You may have guessed I hate writing the same things multiple times)
To filter out the "alphabets", I'd probably sniff the events or similar, depending on how my research into the documentation of textedit went.
Bookmarks