views:

180

answers:

1

I need to get the bounding box (in scene space) of QGraphicsItems that have the QGraphicsItem::ItemIgnoresTransformations flag set.

According to the docs, you need to use QGraphicsItem::deviceTransform() to do that. I tried this:

// Get the viewport => scene transform
vp_trans = view.viewportTransform();
// Get the item => viewport transform
trans = item.deviceTransform(vp_trans);
// Get the item's bounding box in item's space
bbox = item.boundingRect();
// Map it to viewport space
bbox = trans.mapRect(bbox);
// Map it back to scene's space
bbox = vp_trans.mapRect(bbox);

But something is wrong, the bounding boxes appear smaller and far to the right of the items...

+1  A: 

Just figured it out, QGraphicsView::viewportTransform() doc says "Returns a matrix that maps viewport coordinates to scene coordinates", but in fact it returns the scene to viewport transform.

Inverting vp_trans in the last step fixed my problem.

Luper Rouch