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. Display "Error" in QLineEdit
Qt 6.11 is out! See what's new in the release blog

Display "Error" in QLineEdit

Scheduled Pinned Locked Moved General and Desktop
5 Posts 4 Posters 7.8k 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.
  • N Offline
    N Offline
    newe1
    wrote on last edited by
    #1

    Hi!

    I´d like to display the word "Error" in color "red" in QLineEdit "edit2" if someone enters a number > 1 in QLineEdit "edit1".

    I tried:
    @
    double number = edit1->text().toDouble(&ok);
    ...

    if(number > 1.0)
    {
    QString error = "<span style='color: red'>Error</span>";
    edit2->setText(error);
    }

    //Remark: edit1 and edit2 are introduced as pointers:
    QLineEdit *edit1;
    QLineEdit *edit2;
    @

    which didn´t work. If you see the problem, please tell!

    1 Reply Last reply
    0
    • G Offline
      G Offline
      giesbert
      wrote on last edited by
      #2

      QLineEdit displays plain text, not rich text.
      You can change the colors by using the palette.

      @
      QPalette pal = lineEdit->palette();
      pal.setColor(QPalette::WindowText, Qt::red);
      lineEdit->setpalette(pal);
      lineEdit->setText("Error");
      @

      Nokia Certified Qt Specialist.
      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

      1 Reply Last reply
      0
      • N Offline
        N Offline
        newe1
        wrote on last edited by
        #3

        Thanks Gerolf!

        With little adaptations it worked:
        @
        if(number > 1.0)
        {
        QPalette pal = edit2->palette();
        pal.setColor(QPalette::Text, Qt::red);
        edit2->setPalette(pal);
        edit2->setText("Error");
        }
        @
        Thanks! :-)

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          An alternative would be to use a style sheet on the line edit.

          1 Reply Last reply
          0
          • D Offline
            D Offline
            DenisKormalev
            wrote on last edited by
            #5

            Don't forget to save old palette and return to it, when you will show another message in edit2 :)

            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