Paler
Paler

Reputation: 349

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?

Upvotes: 6

Views: 4132

Answers (1)

pnezis
pnezis

Reputation: 12331

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.

Upvotes: 3

Related Questions