Important: Please read the Qt Code of Conduct -

[Solved]QRegExpValidator Questions

  • Hi

    I have a QLineEdit and to prevent user from entering invalid entries i am using a QValidator. I have QLineEdits where only an integer can be entered and where a float can be entered.

    I found QDoubleValidator and QIntValidator but these had some features i didn't liked. For example user can enter dots to a QLineEdit with QIntValidator, which i don't want. Also user can enter dots and commas to a QLineEdit with QDoubleValidator.

    So i decided to use QRegExpValidator. For int validating i used

    validator = new QRegExpValidator(QRegExp("([-]{0,1})([0-9]{0,9})"));

    And for float validating

    validator = new QRegExpValidator(QRegExp("([-]{0,1})([0-9]{0,9})([.]{0,1}[0-9]{0,7})"));

    I have two questions regarding these

    1. Can i make it so user can not enter a number bigger or smaller than a value, like it does with QIntValidator?
    2. The line for float validating i created causes a problem. Currently user can enter fraction numbers without entering a dot first. How can i overcome this, so that user have to enter a dot before entering fraction numbers.

    Thanks in advance

  • Lifetime Qt Champion


    Why not use a QSpinBox and a QDoubleSpin box for the numerical inputs ?

  • @SGaist Thank you very much, it really looks like what i was searching for. I will come back if any come across any problems :)

  • Lifetime Qt Champion

    You're welcome !

    Sure thing

    Happy coding !

Log in to reply