3

我需要获取设置了标志的QGraphicsItems的边界框(在场景空间中)。QGraphicsItem::ItemIgnoresTransformations

根据文档,您需要使用QGraphicsItem::deviceTransform()来做到这一点。我试过这个:

// 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);

但是出了点问题,边界框看起来更小,并且在项目的右侧很远......

4

1 回答 1

4

刚刚弄清楚, QGraphicsView::viewportTransform() 文档说“返回一个将视口坐标映射到场景坐标的矩阵”,但实际上它将场景返回到视口变换。

在最后一步中反转 vp_trans 解决了我的问题。

于 2009-11-12T14:17:40.543 回答