Setting style sheet for QLineEdit affects the tooltip as well
-
This is using Qt 5.15.5 on Linux Ubuntu 18.04.
I am having problems with tooltips turning red after I set the text color to red in a QLineEdit control when validation is not
Acceptable
. I am doing this with a stylesheet, and the value of that is simply the string:color:red
orcolor:black
as the case may be. This works as intended, but it also affects any tooltips attached to the control.Style sheets seem to be the way to go, but since they only appear when hovering over a control, and they seem to inherit whatever is in the control triggering the tooltip, I assume that I would need to change the string for the stylesheet to include some kind of CSS selector for the tooltip. When I set the style for tooltips globally in the application, it seems to have no effect on the specific control since they are always red if the text of the line edit control is also red.
How should I be doing this? I have a lot of tool tips, and although I could probably hard-code the color into the tooltip text, I'd rather have a programmatic solution so that this doesn't complicate the translating of the tooltip texts.
-
Hi,
Your stylesheet have to be explicit otherwise the cascading rule means that the last one overrule the previous one so if you set
color: red
on your line edit, it will apply to all its child widgets. -
In the meantime, it seems that a tooltip is implemented by the undocumented (??)
QTipLabel
class. When I use this style sheet as shown below, it works as desired. Here is the little snippet of my code:void DlgSettingsDialog::on_edtDbDefaultFolder_textChanged(const QString &) { const QString ss = "QLineEdit {%1} QTipLabel {color:black}"; if (ui->edtDbDefaultFolder->hasAcceptableInput()) { ui->edtDbDefaultFolder->setStyleSheet(ss.arg("color:black")); } else { ui->edtDbDefaultFolder->setStyleSheet(ss.arg("color:red")); } setDirty(); }
QTiplLabel
is defined in the fileqtooltip.cpp
in the Qt sources, but I didn't see any reference to it in the help files.. -
Hi,
Your stylesheet have to be explicit otherwise the cascading rule means that the last one overrule the previous one so if you set
color: red
on your line edit, it will apply to all its child widgets.@SGaist Thanks for pointing out that the tooltip is a child of the QLineEdit control. I then subclassed QLineEdit in order to override the
focusInEvent()
and iterated over the child widgets using themetaObject::className()
function to see what things were children of this control. Since I also had theclear
button enabled, I had three children without the tooltip, and four when it was showing.I suspected as much, but wasn't sure what was actually going on.