2

I want to have a QLineEdit that accepts only a character or digit . Is there a possibility to set it like in html an input to have a maxlength ? I mean to do this from the constructor of QLineEdit ?

I don't need something complicated ...

Erty Seidohl
  • 4,487
  • 3
  • 33
  • 45
user3009269
  • 442
  • 6
  • 14

3 Answers3

4

Everything is exactly like you wanted it to be. QLineEdit has the maxLength property. You set it either using the property system, or using the setter method:

QLineEdit le;
le.setMaxLength(1);

That's it.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
3

I don't need someting complicated ...

Unfortunately, it's couldn't be called easy solution, but you should be advised about QRegExpValidator.

Usage example:

#include <QRegExpValidaor>
#include <QLineEdit>
...
...
QRegExp rx ("\\w");
QRegExpValidator * v = new QRegExpValidator (rx, this);
QLineEdit * le = new QLineEdit (this);
le->setValidator (v);
Gluttton
  • 5,739
  • 3
  • 31
  • 58
3

Here is the alternate way to limit line edit input to one character/digit:

QLineEdit le;
le.setInputMask("N");
le.show();

For more details on input mask usage refer to the Qt documentation.

vahancho
  • 20,808
  • 3
  • 47
  • 55