Qt:使用 QTransform 对象进行旋转与在 QGraphicsItem 中设置 setRotation() 不同
设置 QGraphicsItem
旋转时,使用 setRotation()
并使用:
transform = QTransform()
transform.rotate(myAngle)
myItem.setTransform(transform)
在代码的两部分中,我将 setTransformOriginPoint()
设置为同一点。
结果是:
- 使用
setRotation()
方法时,项目在其变换原点上旋转。 - 使用
QTransform
对象时,项目会根据项目的原点(即点 (0,0))旋转。
我的代码比这更复杂,但我认为它同样适用。 QGraphicsItem
实际上是一个 QGraphicsItemGroup
,我可以检查仅添加一项的问题,并在我的旋转过程中更改 setRotation()
方法对于QTransform
对象。后者忽略 setTransformOriginPoint()
。
我遇到这个问题有一段时间了,我挖掘了很多资源。我浏览 Qt C++ 代码,可以看到 setRotation()
方法修改了调用 rotation
的字段(一个真实值) QGraphicsItem
中的 TransformData
结构。原点也是一个两个字段实值,在这样的结构中分别称为xOrigin
和yOrigin
。转换结果存储在 tranform
字段中。所有这些信息都用在一个名为:transformData
的变量中。
所以,我不明白为什么 transformData->transform
字段中的转换集忽略了 transformData->xOrigin
和 transformData->xOrigin 值。 yOrigin
在应用时。
我用来测试该问题的代码是以下相关部分(我有一个旋转项目,它接收鼠标输入并对项目本身应用旋转):
# This method using QTransform object....
def mouseMoveEvent(self, event):
if self.pressed:
parent = self.parentItem()
parentPos = parent.boundingRect().center()
newPoint = event.scenePos()
iNumber = (newPoint.x()-parentPos.x())-((newPoint.y()-parentPos.y()))*1j
angle = cmath.phase(iNumber)+1.5*math.pi
self.appliedRotation = (360-math.degrees(angle))%360 - self.angleOffset
transform = QTransform()
transform.rotate(self.appliedRotation)
self.parentItem().setTransform(transform)
# ...Against this one using setRotation()
def mouseMoveEvent(self, event):
if self.pressed:
parent = self.parentItem()
parentPos = parent.boundingRect().center()
newPoint = event.scenePos()
iNumber = (newPoint.x()-parentPos.x())-((newPoint.y()-parentPos.y()))*1j
angle = cmath.phase(iNumber)+1.5*math.pi
self.appliedRotation = (360-math.degrees(angle))%360 - self.angleOffset
self.parentItem().setRotation(self.appliedRotation)
在这两个项目上,之前都是 setTransformOriginPoint() 已设置,但它不是显示代码的相关部分,而只是知道它已完成。
我因找不到解决方案而感到沮丧。看起来很简单,为什么设置旋转变换矩阵不使用我设置的变换原点,而使用 setRotation() 方法却可以正常工作?这个问题把我带到了源代码,但现在更令人困惑,因为旋转与应用的变换保持分离......
While setting a QGraphicsItem
rotation, I get different results upon the transformation origin point while using setRotation()
and using:
transform = QTransform()
transform.rotate(myAngle)
myItem.setTransform(transform)
In both portion of code, I set setTransformOriginPoint()
to the same point.
Results are:
- While using
setRotation()
method, the item is rotated upon its transformation origin point. - While using the
QTransform
object, the item is rotated upon item's origin, that is, point (0,0).
My code is more complex than that, but I think It applies the same. The QGraphicsItem
is in fact a QGraphicsItemGroup
and I can check the issue adding just one item, and in my rotation procedure change the setRotation()
method for the QTransform
object. The latter, ignores the setTransformOriginPoint()
.
I'm having this issue for a while, and I dig a lot of resources. I browse the Qt C++ code, and I can see that the setRotation()
method modifies a field calles rotation
(a real value) in the TransformData
structure within the QGraphicsItem
. The origin point is also a two field real value in such a structure called xOrigin
and yOrigin
respectively. The transformation is stored in the tranform
field. All this information is used in a variable called: transformData
.
So, I don't get why the transformation set in the transformData->transform
field is ignoring the values transformData->xOrigin
and transformData->yOrigin
at the time of being applied.
The code I used to test that issue is the following relevant part (I have an rotate item that receives mouse inputs and applies rotation to the item itself):
# This method using QTransform object....
def mouseMoveEvent(self, event):
if self.pressed:
parent = self.parentItem()
parentPos = parent.boundingRect().center()
newPoint = event.scenePos()
iNumber = (newPoint.x()-parentPos.x())-((newPoint.y()-parentPos.y()))*1j
angle = cmath.phase(iNumber)+1.5*math.pi
self.appliedRotation = (360-math.degrees(angle))%360 - self.angleOffset
transform = QTransform()
transform.rotate(self.appliedRotation)
self.parentItem().setTransform(transform)
# ...Against this one using setRotation()
def mouseMoveEvent(self, event):
if self.pressed:
parent = self.parentItem()
parentPos = parent.boundingRect().center()
newPoint = event.scenePos()
iNumber = (newPoint.x()-parentPos.x())-((newPoint.y()-parentPos.y()))*1j
angle = cmath.phase(iNumber)+1.5*math.pi
self.appliedRotation = (360-math.degrees(angle))%360 - self.angleOffset
self.parentItem().setRotation(self.appliedRotation)
On both, previously the setTransformOriginPoint()
is set, but it's not a relevant part to show the code, but just to know that it is done.
I'm getting frustrated to not find a solution to it. As it seems so straightforward, why setting a rotation transformation matrix does not use the transformation origin point that I have set and while using setRotation()
method works fine? That question took me to the source code, but now is more confusing as rotation is keeping separated from the transformation applied...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我正在解决同样的问题。我发现 QGraphicsItem::setTransformOriginPoint() 仅适用于 QGraphicsItem::setRotation()。 QGraphicsItem::setTransform() 会忽略它。
我使用此代码来实现 QTransform() 的相同行为:
I was solving the same problem. I found out that QGraphicsItem::setTransformOriginPoint() is accepted only for QGraphicsItem::setRotation(). It is ignored for QGraphicsItem::setTransform().
I use this code to reach the same behavior for QTransform():