简体   繁体   English

自定义 Qt QGraphicsItem 工具提示

[英]Custom Qt QGraphicsItem tooltip

I'm looking for some ways to implement a simple custom tooltip for QGraphicsItem .我正在寻找一些方法来为QGraphicsItem实现一个简单的自定义工具提示。

I know that I can use setToolTip to set text for tooltip.我知道我可以使用setToolTip来设置工具提示的文本。 Now what I want is to change the text dynamically when the mouse hovers at different parts of a QGraphicsItem object.现在我想要的是当鼠标悬停在QGraphicsItem对象的不同部分时动态更改文本。

What I'm thinking to do is when I get an event QEvent::ToolTip , I change the tooltip text in that event handler.我想做的是当我收到一个事件QEvent::ToolTip ,我会更改该事件处理程序中的工具提示文本。 However, I cannot find an event function that recieve QEvent::ToolTip for QGraphicsItem .但是,我找不到为QGraphicsItem接收QEvent::ToolTip的事件函数。

Or is there some ways to handle an event that mouse hovers for 2 seconds.或者是否有一些方法可以处理鼠标悬停 2 秒的事件。

How can I make it?我怎样才能做到?

You could implement the hoverMoveEvent in your derived QGraphicsItem class, and set the tooltip based on the position within the graphics item您可以在派生的QGraphicsItem类中实现hoverMoveEvent ,并根据图形项中的位置设置工具提示

void MyItem::hoverMoveEvent(QGraphicsSceneHoverEvent* event)
{
    QPointF p = event->pos(); 
    // use p.x() and p.y() to set the tooltip accrdingly, for example:
    if (p.y() < height()/2)
        setTooltip("Upper Half");
    else
        setTooltip("Bottom Half");
}

Notice that you have to enable hover events for your item.请注意,您必须为您的项目 启用悬停事件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM