I've a QGraphicsView
hows shows a QGraphicsScene
which contains a QGraphicsItem
. My Item implements the hoverMoveEvent(...)
method that fires up a QToolTip
.
I want that the tool tip to follows the mouse as it moves above of the item. This however only works if I do one of two things:
QToolTip
s where the first is just a dummy and gets overwritten by the second immediately.rand()
into it's text.This implementation does not work as it should. It lets the tooltip appear but it does not follow the mouse. Just as if it realises that it's content has not changed and that it does not need any update.
void MyCustomItem::hoverMoveEvent(QGraphicsSceneHoverEvent *mouseEvent)
{
QToolTip::showText(mouseEvent->screenPos(), "Tooltip that follows the mouse");
}
This code creates the desired result. The tooltip follows the mouse. The drawback is, that you can see a slight flickering since two tooltips are created.
void MyCustomItem::hoverMoveEvent(QGraphicsSceneHoverEvent *mouseEvent)
{
QToolTip::showText(mouseEvent->screenPos(), "This is not really shown and is just here to make the second tooltip follow the mouse.");
QToolTip::showText(mouseEvent->screenPos(), "Tooltip that follows the mouse");
}
Third, the solution presented here also works. I however don't want to show coordinates. The content of the tooltip is static...
How can I make this work without having the described flickering by creating two tooltips or second update the position of the tip?
QTooltip
is created to disappear as soon as you move the mouse, to not have that behavior you can use a QLabel
and enable the Qt::ToolTip
flag. In your case:
.h
private:
QLabel *label;
.cpp
MyCustomItem::MyCustomItem(QGraphicsItem * parent):QGraphicsItem(parent)
{
label = new QLabel;
label->setWindowFlag(Qt::ToolTip);
[...]
}
After where you want to display the message, in your case you want to do it in hoverMoveEvent
, you should place the following code.
label->move(event->screenPos());
label->setText("Tooltip that follows the mouse");
if(label->isHidden())
label->show();
And to hide it you must use:
label->hide();
see this: How to make QToolTip message persistent?