QGraphicsView, QGraphicsPath - find closest path ?
-
Hey
I have multiple QGraphicsPath in my view/scene. How can I detect which path is closest to my clicked point?
Regards
Dariusz -
Hi
The easy solution is to use the items bounding box and
loop over the items and check if mouse click was close to one of them.
If the paths can be overlapped, its much more complicated. -
@Dariusz
Well its a more involved task as we want a better hit testing.
However, try with
bool QPainterPath::contains(const QPointF &point)
and see if that takes into account any "holes".
and/or work for your use case. -
@Dariusz
you loop over the items and ask contain(clickpos)
and if yes, thats the one?
Or am i missing something? -
@mrjj said in QGraphicsView, QGraphicsPath - find closest path ?:
@Dariusz
you loop over the items and ask contain(clickpos)
and if yes, thats the one?
Or am i missing something?What happens if 3+ items return true. How would I know which one is closer ?
-
Also another problem with path.contains that I find is that the contains would use this as check area >
https://doc.qt.io/archives/qt-4.8/images/graphicsview-pathitem.png
Which means that the shape can be bigger/inaccurate comparing to the selection location... I think I will need to do some math and create another method for path click intersection.Ok I sorta managed to "fix" it... I ended up creating a forward and then reverse path during selection process. So that the path is a closed - precise - shape. This way I can get proper click detection, I still cant compute which path is closer, but at least selection works properly now.
1/8