0

QGraphicsItemsユーザーが少数を選択してグループとして回転できるアプリケーションを実装したいと思います。すべてのアイテムを 1 つに追加できることはわかっていますが、各アイテムQGraphicsItemGroupを保持する必要があります。Z-value出来ますか?

2 つ目の質問もあります。私は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)));

2回目の回転の後、アイテムがポイントを中心に回転するのではなく、他のポイントを中心に回転することに気付きました(0,0)(どれかわかりません)。また、操作の順序を変更すると、すべてがうまく機能することにも気付きました。

私は何を間違っていますか?

4

1 に答える 1

0

最初の問題に関して、QGraphicsGroup に入れるときに z 値が問題になるのはなぜですか? 一方、選択したアイテムを繰り返し処理して、変換を適用することもできます。

このスニペットはあなたの2番目の問題を解決すると思います:

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