开发者

Getting the bounding box of a constant size item in Qt graphics views

开发者 https://www.devze.com 2022-12-11 18:43 出处:网络
I need to get the bounding box (in scene space) of QGraphicsItems that have the QGraphicsItem::ItemIgnoresTransformations flag set.

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...


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消