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:
@
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!
-
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");
@ -
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! :-) -
An alternative would be to use a style sheet on the line edit.
-
Don't forget to save old palette and return to it, when you will show another message in edit2 :)