Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QLineEdit - Indicate if user enters an Invalid Value...

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

Scheduled Pinned Locked Moved General and Desktop
4 Posts 4 Posters 5.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Macro
    wrote on last edited by
    #1

    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 Reply Last reply
    0
    • P Offline
      P Offline
      pateusz
      wrote on last edited by
      #2
      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;
      @

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Sam
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved