QGraphicsView, QGraphicsPath - find closest path ?
-
wrote on 25 Oct 2019, 03:26 last edited by
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. -
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. -
@mrjj Problem with that is that it only cares about item pos() and not individual point on the curve... This is somewhat difficult task isnt it ?
@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. -
@mrjj Problem with that is that it only cares about item pos() and not individual point on the curve... This is somewhat difficult task isnt it ?
-
@Dariusz Hmm will give it a go, but that would return either 1 or 1+ paths that get hit, how would I know which path is closest to the hit ?
@Dariusz
you loop over the items and ask contain(clickpos)
and if yes, thats the one?
Or am i missing something? -
@Dariusz
you loop over the items and ask contain(clickpos)
and if yes, thats the one?
Or am i missing something?wrote on 30 Oct 2019, 03:58 last edited by@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 ?
-
@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 ?
wrote on 30 Oct 2019, 04:57 last edited by DariuszAlso 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.
3/8