QGraphicsScene itemAt - allow radius around point?



  • I have polygon objects and I want to detect when the mouse is close to the boundary of the polygon. Currently I use mouseMoveEvent and when the pt is on the border or within 5 pixels inside the border, I determine the border was clicked. I'd like to also say the border has been clicked if within 5 pixels outside the border (where the polygon item's mouseMoveEvent is not triggered). Is this possible? One way I can think of is overriding the mouseMoveEvent in the view or scene and checking each pt within a 5 pixel radius of the mouse position for proximity to a polygon edge, but this seems dirty and inefficient. Is there a better way to do this?



  • Can you override the shape() function of the polygon object to return a slightly larger polygon?



  • How about instead of using itemAt, you simply use the [[doc:QGraphicsScene]] items() with a QRectF argument. The QRectF can be simply constructed to be a small box around the current mouse position.



  • Thanks for the suggestions. I tried using QGraphicsScene::items(QRectF), it seems to work but its a little "messy", the size of my QRectF is in scene units, not each individual polygons' units, although I'm not sure whether that's a bad thing or not...

    As for overriding the shape function, this seems interesting and worth trying. I'm a little concerned as to when else is this function used? Will it be "dangerous" to change its functionality? Will this affect the way the polygon is drawn? I guess I should try it and see...



  • I think that shape() is used only for hit detection and determining when an item needs to be redrawn. It will make your widget appear larger than it really is when the code goes to figure out if it's just been exposed and needs to be redrawn, but that's probably not that big a deal. I don't think it gets used for anything else, but Andre would know better than me!



  • I just tried using the shape function. It worked exactly as desired for boundary hit detection but it seriously messed up the repainting of my other polygons in the scene during mouse events! Putting a breakpoint in the function showed that it was being called all the time and I had to remove the breakpoint. Looks like the items function is working better, I just have to decide what values I'd like to use and do similar things in my scene's hover event to change the cursor when its near the boundary.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.