QGraphicsItem 的 - 选择和选择旋转
我想实现允许用户选择几个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
关于您的第一个问题,为什么将 z 值放入 QGraphicsGroup 时会出现问题?
另一方面,您也可以迭代所选项目并仅应用转换。
我想这个片段会解决你的第二个问题:
编辑:
如果您打算围绕全局坐标系原点旋转,请将旋转更改为:
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:
EDIT:
In case you meant to rotate around the global coordinate frame origin change the rotations to: