Qt does not provide a built-in solution for what you want. You should reimplement QGraphicsScene::mouseMoveEvent
and check in it which point (if any) is hovered (with a certain margin), i.e. determine which point is within a certain distance of the current mouse position (QGraphicsSceneMouseEvent::pos
).
The most computation intensive task is determining the closest point. A naive approach is to iterate over all the points, but general optimised implementation exists:
Caching the last result and using the triangle inequality may be important to improve the performance of this method:
If currently the mouse hovers a point P
, the next time you can just validate if it still hovers this point.
If currently no point is hovered and the nearest point from location P
(the last mouse position for which you calculated the nearest point) is at a distance d
, then you should not check if an hover occur if: norm(P - QGraphicsSceneMouseEvent::pos()) < d - hoverThreshold