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. Setting style sheet for QLineEdit affects the tooltip as well
Forum Update on Monday, May 27th 2025

Setting style sheet for QLineEdit affects the tooltip as well

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 1.0k Views
  • 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.
  • R Offline
    R Offline
    Robert Hairgrove
    wrote on last edited by
    #1

    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 or color: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.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      R 1 Reply Last reply
      3
      • R Offline
        R Offline
        Robert Hairgrove
        wrote on last edited by
        #3

        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 file qtooltip.cpp in the Qt sources, but I didn't see any reference to it in the help files..

        1 Reply Last reply
        0
        • SGaistS SGaist

          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.

          R Offline
          R Offline
          Robert Hairgrove
          wrote on last edited by
          #4

          @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 the metaObject::className() function to see what things were children of this control. Since I also had the clear 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.

          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