1

I want the QLineEdit to accept only numbers without any decimal.e.g it should accept '456' but not '456.3434'.i.e. it should not allow decimal at all. Can anybody give some pointers that how can i do that.

I tried to use QIntValidator, but it still allows to enter decimal point, and when i convert the text from QLinEdit it returns zero (as it documentation says, if the conversion fails it will return zero).

I also tried to use QRegExpValidator( QRegExp("[0-9]"), but it allows only one number. There is no limit for the maximum number, how do i specify the QRegExp with minimum as 0 and maximum as undefined, if QRegExpValidator is the only way to achieve it ?

Thank you

Tunaki
  • 132,869
  • 46
  • 340
  • 423
user1703942
  • 317
  • 3
  • 15

1 Answers1

5

You might try the following validator:

QLineEdit le;
le.setValidator(new QRegExpValidator(QRegExp("[0-9]+")));
le.show();

UPDATE

To allow input in exponential form you can try this:

le.setValidator(new QRegExpValidator( QRegExp("[0-9]+e[0-9]+")));
vahancho
  • 20,808
  • 3
  • 47
  • 55
  • Thank you very much @vahancho. That worked perfectly. What if i want it to accept the number in exponential form as well e.g 1e10 ? – user1703942 Dec 04 '14 at 15:40
  • Thank you @Vahancho. Just one more question, can i set a limit to maximum as well? I want number between 0 to 2000000000 without exponential and decimal point. – user1703942 Dec 05 '14 at 13:34