1. 程式人生 > 實用技巧 >Qt QGraphicsItem物件setPos(),setScale(),setRotation()操作後Item座標和Scene座標的變化

Qt QGraphicsItem物件setPos(),setScale(),setRotation()操作後Item座標和Scene座標的變化

 1     // 建立 item
 2  
 3     CustomItem *pItem = new CustomItem();
 4  
 5     pItem->setRect(20, 20, 60, 60);
 6  
 7  
 8  
 9     // 將 item 新增至場景中
10  
11     CustomScene scene;
12  
13     scene.setSceneRect(0, 0, 400, 300);
14  
15     scene.addItem(pItem);
16  
17    
18  
19     // 為檢視設定場景
20  
21     QGraphicsView view;
22 23 view.setScene(&scene); 24

矩形左上角:

Item pos: QPointF(23,19)

scene pos: QPointF(23,19)

矩形右下角:

Item pos: QPointF(80,79)

scene pos: QPointF(80,79)

注:由於是用點選螢幕相應位置獲取的座標,可能存在一些誤差。

voidQGraphicsItem::setPos(constQPointF&pos)

Sets the position of the item to pos, which is in parent coordinates. For items with no parent, pos is in scene coordinates.

The position of the item describes its origin (local coordinate (0, 0)) in parent coordinates.

一 setPos操作之後的情況

1 pItem->setRect(20, 20, 60, 60);
2 pItem->setPos(50,50);
所以setPos(50,50)是把item座標點(0,0)設定為與scene座標點(50,50)重合,那麼矩形左上角的item座標點為(20,20),則其scene座標點即為(20+50,20+50)=(70,70);矩形右下角的item座標點為(80,80),則其scene座標點即為(80+50,80+50)=(130,130)。

可理解為下圖:

矩形左上角:

Item pos: QPointF(21,20)

scene pos: QPointF(71,70)

矩形右下角:

Item pos: QPointF(78,79)

scene pos: QPointF(128,129)

voidQGraphicsItem::setScale(qrealfactor)

Sets the scale factor of the item. The default scale factor is 1.0 (i.e., the item is not scaled). A scale factor of 0.0 will collapse the item to a single point. If you provide a negative scale factor, the item will be flipped and mirrored (i.e., rotated 180 degrees).

The item is scaled around its transform origin point, which by default is (0, 0). You can select a different transformation origin by callingsetTransformOriginPoint().

The scale is combined with the item'srotation(),transform() andtransformations() to map the item's coordinate system to the parent item.

二setScale()操作之後的情況

1 pItem->setRect(20, 20, 60, 60);
2 pItem->setPos(50,50);
3 pItem->setScale(2);

可以理解為下圖所示:

矩形左上角:

Item pos: QPointF(20.5,20)

scene pos: QPointF(91,90)

矩形右下角:

Item pos: QPointF(79,78)

scene pos: QPointF(208,206)

可以看出setScale(2)後,左上角和右下角的ItemPos並沒有改變。item座標點(0,0)仍然設定為與scene座標點(50,50)重合,矩形左上角的item座標點為(20,20),當放大兩倍後左上角離itempos (0,0)的距離則變為20*2=40,及在scene座標系中,矩形左上角的座標為(50+20*2,50+20*2)=(90,90)。同理,矩形右下角的座標在scene座標系中變為(50+20*2+60*2,50+20*2+60*2)=(210,210)。

三 接著新增setRotation()之後的情況

voidQGraphicsItem::setRotation(qrealangle)

Sets the clockwise rotation angle, in degrees, around the Z axis. The default value is 0 (i.e., the item is not rotated). Assigning a negative value will rotate the item counter-clockwise. Normally the rotation angle is in the range (-360, 360), but it's also possible to assign values outside of this range (e.g., a rotation of 370 degrees is the same as a rotation of 10 degrees).

The item is rotated around its transform origin point, which by default is (0, 0). You can select a different transformation origin by callingsetTransformOriginPoint().

The rotation is combined with the item'sscale(),transform() andtransformations() to map the item's coordinate system to the parent item.

1 pItem->setRect(20, 20, 60, 60);
2 pItem->setPos(50,50);
3 pItem->setScale(2);
4 pItem->setRotation(45);

可以理解為下圖所示:

矩形左上角:

Item pos: QPointF(20.5061,21.2132)

scene pos: QPointF(49,109)

矩形右下角

Item pos: QPointF(78.1353,78.8424)

scene pos: QPointF(49,272)

矩形中心點scenepos: QPointF(50,191.421)

可以看出放大之後旋轉(預設是以itempos(0,0) (即是以scenepos(50,50))為軸進行旋轉),左上角和右下角的ItemPos仍然沒有改變。setScale(2)對於item座標系中除了矩形區域外的其餘部分沒有影響。旋轉之後,矩形在scene座標系中的座標發生了大幅度變化,因為scene座標系並沒有旋轉。在scene座標系中,矩形左上角座標即為(50,50+20*sin45*2)=(50,106.569),右下角座標為(50,106.569+60*2*sin45*2)=(50,276.274)。

四 接著setTransformOriginPoint()後的情況

voidQGraphicsItem::setTransformOriginPoint(constQPointF&origin)

Sets the origin point for the transformation in item coordinates.

1 pItem->setRect(20, 20, 60, 60);
2 pItem->setPos(50,50);
3 pItem->setScale(2);
4 pItem->setRotation(45);
5 pItem->setTransformOriginPoint(20,20);

可理解為下圖所示:

矩形左上角:

Item pos: QPointF(20.3536,21.0607)

scene pos: QPointF(69,72)

矩形右下角:

Item pos: QPointF(78.6899,78.6899)

scene pos: QPointF(70,236)

矩形中心點scenepos: QPointF(70,154.853)

可以看出修改TransformOriginPoint之後,新的TransformOriginPoint(20,20)與原本的預設TransformOriginPoint(0,0)之間的那一部分將不再受到pItem->setRotation(45)旋轉的影響,而(20,20)之後的區域仍然基於新的變換原點正常變化。所以,在scene座標系中,矩形左上角的座標為(50+20,50+20)=(70,70),矩形右下角的座標為(50+20,50+20+60*2*sin45*2)約等於(70,240)。

1 pItem->setPos(50,50);
2 pItem->setScale(2);
3 pItem->setRotation(45);
4 pItem->setTransformOriginPoint(50,50);

可理解為示意圖如下:

矩形左上角

Item pos: QPointF(20.3015,21.0086)

scene pos: QPointF(99,17)

矩形右下角

Item pos: QPointF(78.9914,78.9914)

scene pos: QPointF(100,182)

矩形中心點scenepos: QPointF(100,100)

可以看出setTransformOriginPoint(50,50)之後是以矩形的中心(縮放旋轉前最初的中心座標即為(50,50))為旋轉軸心進行旋轉和縮放的,且矩形中心的scenepos為(50+50,50+50)=(100,100)。則旋轉縮放後在scene座標系中,矩形的各點座標可根據其相對於矩形中心的距離來求得,例如矩形左上角的座標即為(100,100-60*2*sin45)=(100,15.147),矩形右下角的座標即為(100,100+60*2*sin45)=(100,184.8)。