QGraphicsItem has two related functions that you are interested in. The first is boundingRect . This, as you probably understand, is a rectangle that spans the entire element. Qt uses this for things like quick calculation of how much of an element is visible and a simple collision of elements.
Great if you have rectangular elements; you can simply override boundingRect () on any elements that you inherit from QGraphicsItem or QGraphicsObject.
If you have a form that is not regular, and you want to do things like colliding with the shape of an element, then the shape () function also requires an override in your class.
This returns QPainterPath, so you can do something like this: -
QPainterPath Line::shape() { QRectF rect(start_p, end_p).normalized();
Now you can use the painter to draw the shape () element, instead of boundingRect (), and collisions will work as expected.
source share