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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With