關於你的第一個問題,爲什麼z值在把它們放到QGraphicsGroup中時會成爲問題? 另一方面,你也可以遍歷選定的項目,並應用轉換。
我想這個片段將解決你的第二個問題:
QGraphicsView view;
QGraphicsScene scene;
QPointF itemPosToRotate(-35,-35);
QPointF pivotPoint(25,25);
QGraphicsEllipseItem *pivotCircle = scene.addEllipse(-2.5,-2.5,5,5);
pivotCircle->setPos(pivotPoint);
QGraphicsRectItem *rect = scene.addRect(-5,-5,10,10);
rect->setPos(itemPosToRotate);
// draw some coordinate frame lines
scene.addLine(-100,0,100,0);
scene.addLine(0,100,0,-100);
// do half-cicle rotation
for(int j=0;j<=5;j++)
for(int i=1;i<=20;i++) {
rect = scene.addRect(-5,-5,10,10);
rect->setPos(itemPosToRotate);
QPointF itemCenter = rect->pos();
QPointF pivot = pivotCircle->pos() - itemCenter;
// your local rotation
rect->setRotation(45);
// your rotation around the pivot
rect->setTransform(QTransform().translate(pivot.x(), pivot.y()).rotate(180.0 * (qreal)i/20.0).translate(-pivot.x(),-pivot.y()),true);
}
view.setScene(&scene);
view.setTransform(view.transform().scale(2,2));
view.show();
編輯: 如果你的意思是沿全局座標系原點改變旋轉旋轉:
rect->setTransform(QTransform().translate(-itemCenter.x(), -itemCenter.y()).rotate(360.0 * (qreal)j/5.0).translate(itemCenter.x(),itemCenter.y()));
rect->setTransform(QTransform().translate(pivot.x(), pivot.y()).rotate(180.0 * (qreal)i/20.0).translate(-pivot.x(),-pivot.y()),true);
讓我們說我在我的場景(名稱= Z值) (item1 = 1,item2 = 2,item3 = 3,item4 = 4) item3 = 3)] = 5 此操作n將使item1和item3將會到達我的場景的頂部。無論物品是否分組,我都希望場景的「堆疊層」不變。 –
我不明白你實際上想要做什麼... 要麼繪製一張圖片,要麼精確地改寫你的問題。 我發佈的代碼做了你想要的旋轉(你的第二個問題),除非「再一次,但是現在圍繞(0,0)」你的意思是關於全局座標系的原點。 在這種情況下,只需做類似於樞軸旋轉的另一個旋轉,但將樞軸更改爲(0,0)。 我仍然不知道你的意思是「堆棧層不變......」。 你想旋轉多個物體並保持其局部旋轉嗎? – pokey909