Multicolor tooltip in Qt

让人想犯罪 __ 提交于 2019-12-14 03:52:05

问题


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?


回答1:


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>");

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.



来源:https://stackoverflow.com/questions/16146211/multicolor-tooltip-in-qt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!