0

我想实现允许用户选择几个QGraphicsItems然后将它们作为一个组旋转的应用程序。我知道我可以将所有项目添加到一个中QGraphicsItemGroup,但我需要保留Z-value每个项目。可能吗?

我还有第二个问题。我试图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)而是围绕其他点旋转(我不知道是哪个点)。我还注意到,如果我改变操作顺序,一切都会很好。

我究竟做错了什么?

4

1 回答 1

0

Regarding your first problem, why should the z-values be a problem when putting them into a QGraphicsGroup? On the other hand you could also iterate through the selected items and just apply the transformation.

I guess this snippet will solve your 2nd problem:

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

EDIT: In case you meant to rotate around the global coordinate frame origin change the rotations to:

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);
于 2011-07-04T19:52:27.737 回答