Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Qt QGraphicsItem tooltip

Tags:

c++

qt

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?

like image 596
Paler Avatar asked Oct 19 '22 11:10

Paler


1 Answers

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.

like image 144
pnezis Avatar answered Oct 21 '22 06:10

pnezis