Search code examples
c++qtqlineedit

Set QLineEdit to accept only numbers


I have a QLineEdit where the user should input only numbers.

So is there a numbers-only setting for QLineEdit?


Solution

  • QLineEdit::setValidator(), for example:

    myLineEdit->setValidator( new QIntValidator(0, 100, this) );
    

    or

    myLineEdit->setValidator( new QDoubleValidator(0, 100, 2, this) );
    

    See: QIntValidator, QDoubleValidator, QLineEdit::setValidator