5

I want to have moveable multicolor tooltip for QGraphicsItems. For example when I click on graphicsItem in scene, tooltip appears, then during dragging mouse tooltip should follow cursor. I can implement movable tooltip with standard QToolTip, but seems Qt support only 1 color for whole tooltip. Also QToolTip doesn't have paintEvent, so I decide to create ColoredTooltip class inherited from QTextEdit, but problem appears when I show at first time ColoredTooltip object. It began to grab mouse events, which is not ok for me, because I can't catch mouseMove events for graphics scene and move coloredTooltip. How can I solve that problem?

IKM2007
  • 716
  • 1
  • 8
  • 31
  • Did you mean "multiple colors on same tooltip"? Do you want to change the color or do you want to "texturize" the tool tip? – leemes Apr 22 '13 at 11:41
  • No, I can change color for whole tooltip, but I want to have at least 3 colors in single tooltip. – IKM2007 Apr 22 '13 at 11:53
  • And how should they appear? As a linear gradient? Did you try the brush as in my answer? I didn't test it, so I don't know if that one works. – leemes Apr 22 '13 at 11:54
  • Ah wait... If you mean the text color, then you most probably want to color *words*. You can use HTML in a tool tip. – leemes Apr 22 '13 at 11:58
  • Html not work in standard tooltip. – IKM2007 Apr 22 '13 at 12:00
  • HTML works for me, see my other answer. Please try that one. – leemes Apr 22 '13 at 12:03
  • I use QToolTip::showText and html not work. Since graphicsItems created and deleted all the time of scrolling scene, I not set tooltip for items, I show tooltip text in cursor current position. – IKM2007 Apr 22 '13 at 12:12
  • So you don't want to try my solution? It works, I even attached a screenshot. I also don't see how you move this tool tip with the mouse while showing when using the static `showText` – leemes Apr 22 '13 at 12:14
  • So I need to use setToolTip instead of QToolTip::showText? – IKM2007 Apr 22 '13 at 12:16
  • Just try it, then we'll see how the performance is. I mean, it's one line of code, isn't it? – leemes Apr 22 '13 at 12:16
  • No, I made some change in design for trying your solution :) First time it not work, but after removing /t /n from tooltip text if works! Thanks a lot! – IKM2007 Apr 22 '13 at 12:47
  • 1
    Oh, I see. Note that once the text contains HTML tags, Qt parses it as HTML (they call it "rich text"). In HTML, whitespace characters like `\n` and `\t` are ignored. Use `
    ` for line breaks. For tabs, I don't know of any such replacement. Usually, one uses CSS to do this, but this might be some overkill here (I also remember having some troubles with padding in Qt CSS). Maybe use good old and ugly spaces for this `    `
    – leemes Apr 22 '13 at 12:52

1 Answers1

9

To color single words (or any part of a text) use Qt's tiny HTML subset supported by its rich text engine. The <font> tag and the attribute color= are supported. QToolTip supports rich text.

This one works for me:

toolTip.setText("foo <font color=\"red\">bar</font>");

To use any RGB color (not only known names), use the hex-notation:

toolTip.setText("foo <font color=\"#ff0000\">bar</font>");

Image showing a tool tip where one word is colored in red

Note that unless you need to move the tool tip while being shown or want to set additional properties, you can simply use QGraphicsItem::setToolTip.

leemes
  • 44,967
  • 21
  • 135
  • 183