QLineEdit - Indicate if user enters an Invalid Value...



  • Hi...

    I have a QLineEdit in which the user can enter only an Integer. I used a QRegExpression validator instead of QIntValidator for the integer. Because the user can enter any value which can be out of range. But I want to introduce another validator to highlight if the user enters a value which is out of specified range. Like if the value is within the range, the font color should be in green otherwise red.

    Please suggest me.

    Thanks and Regards.



    1. Why not to use QSpinBox?
    2. In your case you should make a slot which will check data, and connect it with textEdited signal

    slot wiil be something like this:
    @if (lineedit->text()->toInt() > x || lineedit->text()->toInt() < Y)
    do sth;
    @



  • You can have a look in Qt Stylesheets for representing the number and change the font color if there is an error. A simple example would be "-

    @if (error)
    {
    lineEdit->setProperty("error",true);
    lineEdit->style()->unpolish(lineEdit);
    lineEdit->style()->polish(lineEdit);
    lineEdit->update();

    }@
    and in the stylesheet file you can specify the style as

    @QLineEdit#yourLineEdit[error="true"]
    {
    color: red;
    }

    QLineEdit#yourLineEdit[error="false"]
    {
    color: green;
    }
    @

    You need to force the update based on the error = true or false.

    Written brain to terminal not tested


  • Lifetime Qt Champion

    But why do you allow out of range values to be entered ?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.