简体   繁体   中英

Custom Qt QGraphicsItem tooltip

I'm looking for some ways to implement a simple custom tooltip for QGraphicsItem .

I know that I can use setToolTip to set text for tooltip. Now what I want is to change the text dynamically when the mouse hovers at different parts of a QGraphicsItem object.

What I'm thinking to do is when I get an event QEvent::ToolTip , I change the tooltip text in that event handler. However, I cannot find an event function that recieve QEvent::ToolTip for QGraphicsItem .

Or is there some ways to handle an event that mouse hovers for 2 seconds.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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