PyQt QGraphicsEllipseItem 旋转偏移

3

我刚接触PyQt,遇到了一个问题,即旋转QGraphicsEllipseItem时出现了问题。我希望椭圆绕其中心旋转,而不是绕用于定义椭圆的QRectF的角旋转。我的代码如下(抱歉,我编写代码的计算机没有Internet访问权限,因此我手动将相关部分复制到这里):

self.scene = QtGui.QGraphicsScene()
self.ui.graphicsView.setScene(self.scene)
pen = QtGui.QPen(QColor(Qt.yellow))

# Draw first Ellipse
# This code correctly places a yellow ellipse centered at the scene 500,500 point
ellipse1 = QtGui.QGraphicsEllipseItem(0,0,100,10)
ellipse1.setPen(pen)
self.scene.addItem(ellipse1)
ellipse1.setPos(500, 500)
ellipse1.translate(-50, -5)

# Now, try to draw a rotated ellipse
# This code rotates the ellipse about the 0,0 location of the rectangle 
#      which is the scene 450, 495 point, not the center of the ellipse
ellipse2 = QtGui.QGraphicsEllipseItem(0,0,100,10)
ellipse2.setPen(pen)
self.scene.addItem(ellipse2)
ellipse2.setPos(500, 500)
ellipse2.translate(-50, -5)
ellipse2.rotate(45.0)

好的,这基本上是我预期的。由于QGraphicsEllipseItem源自QGraphicsItem,因此在旋转之前,我尝试为ellipse2设置变换原点:

ellipse2 = QtGui.QGraphicsEllipseItem(0,0,100,10)
ellipse2.setPen(pen)
self.scene.addItem(ellipse2)
ellipse2.setPos(500, 500)
ellipse2.translate(-50, -5)
ellipse2.setTransformOriginPoint(450, 495)
ellipse2.rotate(45.0)

这导致出现错误“AttributeError:'QGraphicsEllipseItem'对象没有'setTransformOriginPoint'属性”。
显然,我在做一些错误的事情或对QGraphicsEllipseItem做了错误的假设。一些站点暗示我可能需要使用边界矩形来进行旋转,但我不明白如何实现。
如果有人能向我展示在pyqt中正确地围绕椭圆的中心旋转的方法,我将不胜感激!!!
2个回答

3

我曾经遇到过同样的问题,并花了整整两天才解决它。
这是我的解决方案:
首先,您应该定义椭圆围绕其旋转的点的坐标(x,y),如下所示:
ellipse.setTransformOriginPoint(QPointF(?, ?))
然后,您可以使用setRotation()来旋转椭圆形
完整的代码如下所示:

__author__ = 'shahryar_slg'

from PyQt4.QtGui import *
from PyQt4.QtCore import *


class MainWindow(QDialog):

    def __init__(self):
        super(QDialog, self).__init__()

        self.view = QGraphicsView()
        self.scene = QGraphicsScene()
        self.layout = QGridLayout()
        self.layout.addWidget(self.view, 0, 0)
        self.view.setScene(self.scene)
        self.setLayout(self.layout)


        self.ellipse = QGraphicsEllipseItem(10, 20, 100, 60)
        self.ellipse.setTransformOriginPoint(QPointF(100/2+10, 60/2+20)) 
        self.ellipse.setRotation(-60)
        self.scene.addItem(self.ellipse)

        # I created another ellipse on the same position to show you
        # that the first one is rotated around it's center:
        self.ellipse2 = QGraphicsEllipseItem(20, 20, 100, 40)
        self.scene.addItem(self.ellipse2)

        self.update()


if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    app.exec_()

请注意我计算椭圆中心的方式。


3

好的,经过多周的努力,我终于找到了自己的答案,虽然我并不真正理解它为什么有效。这是我通过盲文编程的标准方法。无论如何,代码应该如下所示:

transform = QtGui.QTransform()
ellipse = QtGui.QGraphicsEllipseItem(0,0,100,10)
ellipse.setPen(pen)
ellipse.setPos(500, 500)
transform.rotate(-45.0)  # rotate the negative of the angle desired
transform.translate((-50, -5) # center of the ellipse
ellipse.setTansform(transform)
self.scene.addItem(ellipse)

这样可以成功地将旋转后的椭圆中心放置在点500,500。我不确定为什么要取旋转角度的负值,但它似乎起作用。如果有人能解释一下为什么会这样,请告诉我。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接