QToolTip and QLineEdit
-
I am having using QLineEdit and QToolTip. Based upon certain conditions I need to change the background color of the tool tip to red. I have tried using setStyleSheet as below:
m_pGlobal->LaunchPage.latitudeSecVal->setStyleSheet("QLineEdit { color: black; background-color: lightgray; }"); m_pGlobal->LaunchPage.latitudeSecVal->setStyleSheet("QToolTip { color: white; background-color: red; }");
and the tooltip works but the QLineEdit style is changed. The background color is picked up from the parent.
I also tried using a QPalette as below:
QPalette palette = QToolTip::palette(); palette.setColor(QPalette::ToolTipBase, Qt::red); palette.setColor(QPalette::ToolTipText, Qt::white); QToolTip::setPalette(palette);
Using this doesn't affect the style of the QLineEdit but background color of the ToolTipBase should be red but it is not there. The text is the right color.
Is there something else I haven't tried?
-
I have found a solution to my problem. I use CSS in my styleSheet to figure this out. I have posted the style sheet below.
QLineEdit { color: black; background-color: lightgray;} QToolTip { border: 0px; background-color: black; color: white;} QLineEdit[cssClass~="error"] { border: 2px solid red; background-color: lightgray; color: black; } QLineEdit[cssClass~="error"] QToolTip { border: 0px; background-color: red; color: white;}
I then set the Property if the item in question. Setting the cssClass for an error or blank for plain.
line->setProperty ( "cssClass", "error" );
or
line->setProperty ( "cssClass", "" );
-
Hi,
For the palette case, the style used can ignore it to ensure the application follows the OS standard.
As for the stylesheet, one issue is that the second call replaces what you set with the first one.
You need to set one stylesheet that contains all the changes you want to apply to that widget. -
I have found a solution to my problem. I use CSS in my styleSheet to figure this out. I have posted the style sheet below.
QLineEdit { color: black; background-color: lightgray;} QToolTip { border: 0px; background-color: black; color: white;} QLineEdit[cssClass~="error"] { border: 2px solid red; background-color: lightgray; color: black; } QLineEdit[cssClass~="error"] QToolTip { border: 0px; background-color: red; color: white;}
I then set the Property if the item in question. Setting the cssClass for an error or blank for plain.
line->setProperty ( "cssClass", "error" );
or
line->setProperty ( "cssClass", "" );
-
-
@gabello306
Don't forget you should polish the style after this dynamicsetProperty()
change.QStyle::unpolish()
/polish()
,QWidget::ensurePolished()
. Else it may not update immediately.