gpt4 book ai didi

python - 如何在 QGraphicsItem 上使用 QGraphicsRotation

转载 作者:行者123 更新时间:2023-12-01 05:51:27 26 4
gpt4 key购买 nike

我有一个QGraphicsItem ( self .rb)。我想使用 QGraphicsRotation 围绕一个点(self.originPoint)旋转这个项目。但是当我尝试执行下面的代码时,它给出了如下所示的错误。有人可以帮忙吗?

in canvasMoveEvent self.rb.setTransform(rotationItem) TypeError: QGraphicsItem.setTransform(QTransform, bool combine=False): argument 1 has unexpected type 'QGraphicsRotation'

Python version: 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)]

代码:

def canvasPressEvent(self, event):
self.rb = QgsRubberBand(self.canvas, True)
self.originPoint = self.rb.boundingRect().center()
if self.rb:return

def canvasMoveEvent(self,event):
if not self.rb:return
currpoint = self.toLayerCoordinates(self.layer,event.pos())
angle = center.azimuth( currpoint )

rotationItem = QGraphicsRotation()
rotationItem.setAngle = angle
rotationItem.setOrigin = QVector3D(self.originPoint)
self.rb.setTransform(rotationItem)

最佳答案

QGraphicsItem.setTransform()函数需要 QTransform作为它的第一个参数。让你措手不及的是 QGraphicsRotation不是 QTransform!它处于不同的层次结构中;它继承自QGraphicsTransform ,继承自 QObject .

所以你不能传入 QGraphicsRotation 来旋转你的 QGraphicsItem,这是违反直觉的。您必须创建一个 QTransform 来执行您想要的操作,并使用它。

或者 - 这可能更容易 - 使用 QGraphicsItem.setRotation()QGraphicsItem.setTransformOriginPoint()函数来应用您所需的转换,而无需担心自己创建任何转换对象。

希望有帮助!

关于python - 如何在 QGraphicsItem 上使用 QGraphicsRotation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14155465/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com