QGraphicsItem 的 - 选择和选择旋转

发布于 2024-11-18 06:58:21 字数 733 浏览 2 评论 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) 旋转,而是围绕其他点(我不知道哪个)旋转。我还注意到,如果我改变操作顺序,一切都会很好。

我做错了什么?

I'd like to implement application which allows user to select few QGraphicsItems and then rotate them as a group. I know that I could add all items into one QGraphicsItemGroup but I need to keep Z-value of each item. Is it possible?

I also have a second question.
I'm trying to rotate QGraphicsItem around some point (different from (0,0) - let's say (200,150)). After that operation I want to rotate this item once more time but now around (0,0). I'm using code below:

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

I noticed that after second rotation the item is not rotated around point (0,0) but around some other point (I don't know which). I also noticed that if I change order of operations it all works great.

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓦然回首 2024-11-25 06:58:21

关于您的第一个问题,为什么将 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);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文