0

I have a QGraphicsPathItem which is drawn from a list of cartesian x,y points.

What would be the best (performance wise) method of determining when the cursor is hovered over one of these points I presently iterate through the source list and compare each point with the cursor position.

Regards

retarded
  • 127
  • 1
  • 10
  • How many `QGraphicsPathItem` do you have? One for each point or one for all the points together? A [mcve] may be useful. – m7913d Jun 06 '17 at 16:12
  • Each path represent a trail consisting of hundreds of waypoints. So essentially I want to determine when I'm hovered on a waypoint. – retarded Jun 06 '17 at 17:38

2 Answers2

0

I usually use the QGraphicScene's itemAt() method to check for graphic items under the cursor.

Ronaldo Nazarea
  • 356
  • 1
  • 7
0

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

m7913d
  • 10,244
  • 7
  • 28
  • 56