How to do precise mouse event handling in QGraphicsScene when ItemClipsChildrenToShape is enabled?
I have a QGraphicsItem that clips its child items (I enabled its ItemClipsChildrenToShape flag). I noticed that clipping makes assigning the mouse event to the children items imprecise: instead of the precise shape of the items their bounding rectangles are used for detecting which item开发者_StackOverflow is located at the specific position so the children receive mouse events in their whole bounding rectangle. When clipping is not enabled it works fine as expected.
Setting the bounding region granularity of the child items to 1.0 didn't help. I'm using qt 4.5.0.
The program that I tested this issue with is available at http://pastebin.com/m3d0cfb53
I could not find anything about this behaviour in the qt docs. I'd like to know whether it's a bug in qt and whether there's a workaround for it.
I know this is an old question, but I ran into the same problem.
The documentation for QGraphicsItems says:
The
shape()
function is used for many things, including collision detection, hit tests, and for the QGraphicsScene::items() functions.The default implementation calls boundingRect() to return a simple rectangular shape, but subclasses can reimplement this function to return a more accurate shape for non-rectangular items.
So overriding the shape()
function with a QPainterPath fixed the problem for me.
精彩评论