在Qt的多色工具提示 [英] Multicolor tooltip in Qt

查看:206
本文介绍了在Qt的多色工具提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为QGraphicsItems添加可移动的多色工具提示。例如,当我点击场景中的graphicsItem,工具提示出现,然后在拖动鼠标工具提示应该跟随光标。我可以实现与标准的QToolTip的可移动工具提示,但似乎Qt只支持1颜色整个工具提示。还有QToolTip没有paintEvent,所以我决定创建ColoredTooltip类继承自QTextEdit,但问题出现时,我第一次显示ColoredTooltip对象。它开始抓住鼠标事件,这对我来说不行,因为我无法捕捉mouseMove事件的图形场景和移动coloredTooltip。

要对单个单词(或文本的任何部分)着色,请使用。


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?

解决方案

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.

这篇关于在Qt的多色工具提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆