QGraphicsItem的 - 选择和旋转
问题描述:
我想实现应用程序,它允许用户选择几个QGraphicsItems
,然后将它们作为一个组来旋转。我知道我可以将所有项目添加到一个QGraphicsItemGroup
,但我需要保留每个项目的Z-value
。可能吗?QGraphicsItem的 - 选择和旋转
我还有第二个问题。 我正在尝试围绕某个点旋转QGraphicsItem
(与(0,0)
不同 - 比如说(200,150)
)。在那之后,我想再次旋转这个项目,但是现在在(0,0)
左右。我使用的代码如下:
QPointF point(200,150); // point is (200,150) at first time and then it is changed to (0,0) - no matter how...
qreal x = temp.rx();
qreal y = temp.ry();
item->setTransform(item->transform()*(QTransform().translate(x,y).rotate(angle).translate(-x,-y)));
我注意到,第二次旋转后该项目不在身边点(0,0)
但周围的一些其他点(我不知道是哪个)旋转。我也注意到,如果我改变了操作顺序,它一切都很好。
我在做什么错?
答
关于你的第一个问题,为什么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