April 14, 2011

newe1 newe1
Lab Rat
14 posts

Display “Error” in QLineEdit

 

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:

  1. double number = edit1->text().toDouble(&ok);
  2. ...    
  3.  
  4. if(number > 1.0)
  5. {
  6.      QString error = "<span style='color: red'>Error</span>";
  7.      edit2->setText(error);
  8. }
  9.  
  10. //Remark: edit1 and edit2 are introduced as pointers:
  11. QLineEdit *edit1;
  12. QLineEdit *edit2;

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

4 replies

April 14, 2011

Gerolf Gerolf
Area 51 Engineer
3210 posts

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

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

 Signature 

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

April 14, 2011

newe1 newe1
Lab Rat
14 posts

Thanks Gerolf!

With little adaptations it worked:

  1. if(number > 1.0)
  2. {
  3.          QPalette pal = edit2->palette();
  4.          pal.setColor(QPalette::Text, Qt::red);
  5.          edit2->setPalette(pal);
  6.          edit2->setText("Error");
  7. }

Thanks! :-)

April 14, 2011

Andre Andre
Area 51 Engineer
6031 posts

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

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

April 14, 2011

Denis Kormalev Denis Kormalev
Lab Rat
1654 posts

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

 
  ‹‹ How can i save data (with the same tag) from an xml to a qmap?      QTabBar background and QTabWidgets corner widget ››

You must log in to post a reply. Not a member yet? Register here!