PDA

View Full Version : Enter only numbers in the text edit??



Gokulnathvc
23rd March 2011, 12:51
How to make the text edit to allow for number only?? It should not allow any alphabets to enter on it..

FelixB
23rd March 2011, 12:52
see QValidator

high_flyer
23rd March 2011, 14:28
More specifically QIntValidator.

FelixB
23rd March 2011, 14:53
More specifically QIntValidator.

I would have given him a more specialized information, but unfortunately he didn't specify what kind of numbers he wants to display.

If he wants to display doubles, a QIntValidator would not be very useful ;)

high_flyer
23rd March 2011, 15:04
True, but I took a guess. :)

Lesiok
23rd March 2011, 16:31
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.

high_flyer
23rd March 2011, 16:36
How to make the text edit to allow for number only??

QIntValidator and QDoubleValidator are not good .... numbers that contains digits, +- signs, decimal point sign etc.
-12.234 is still a number.
Since he didn't provide any information about other restrictions all solutions suggested so far MIGHT be correct, but not necessarily.

wysota
23rd March 2011, 16:40
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 :)

squidge
23rd March 2011, 18:01
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.