Lion King
Lion King

Reputation: 33813

How to validate QlineEdit in order to allow to write a string only or allow to write numbers only

I have a QLineEdit.

I want when the user enters data in that field allows only to enter the numbers, or allows only to enter the text string, and so on.

I know the method to do that and that by using void QLineEdit::setValidator (const QValidator * v), but I don't know how to use this method?

Upvotes: 2

Views: 2537

Answers (1)

Jablonski
Jablonski

Reputation: 18504

QRegExp rx("regex");
QValidator *validator = new QRegExpValidator(rx, this);

QLineEdit *edit = new QLineEdit(this);
edit->setValidator(validator);

Where regex for string:

[a-zA-Z]+ 

For numbers:

[0-9]+

Upvotes: 6

Related Questions