2009-11-12 91 views
1

我需要獲取設置了QGraphicsItem::ItemIgnoresTransformations標誌的QGraphicsItem的邊界框(在場景空間中)。在Qt圖形視圖中獲取恆定大小項目的邊界框

根據文檔,您需要使用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); 

可是,我錯了,邊框看起來更小和遠來的物品的權利......

回答

2

就想通了,的QGraphicsView :: viewportTransform()醫生說「 返回一個矩陣,將視口座標映射到場景座標「,但實際上它將返回場景視口變換。

在最後一步反轉vp_trans解決了我的問題。

+0

我遇到了和你一樣的問題,並解決了這個問題。非常感謝! – 2016-03-16 20:12:50