I have a QGraphicsItem(self.rb). I want to rotate this item around a point(self.originPoint) using QGraphicsRotation. But when I try to execute the below code, it gives me the error shown below. Can someone help?

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

Code:

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)
有帮助吗?

解决方案

The QGraphicsItem.setTransform() function takes a QTransform as its first argument. What's catching you off guard is that a QGraphicsRotation isn't a QTransform! It's in a different hierarchy; it inherits from QGraphicsTransform, which inherits from QObject.

So you can't pass in a QGraphicsRotation to rotate your QGraphicsItem, counterintuitive as that is. You'll have to make a QTransform that does what you want, and use that.

Or - and this is probably a lot easier - use the QGraphicsItem.setRotation() and QGraphicsItem.setTransformOriginPoint() functions to apply your desired transformation without worrying about creating any transformation objects yourself.

Hope that helps!

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top