Вопрос

New to PyQt and I'm having an issue rotating a QGraphicsEllipseItem. I want the ellipse to rotate around the center of the ellipse instead of the corner of the QRectF used to define the ellipse. My code looks like this (sorry, the computer I am coding it on, doesn't have internet access, so I am copying the relevant parts here by hand):

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)

OK, that is basically what I expected. Since QGraphicsEllipseItem is derived from QGraphicsItem, I tried to set the transform origin point for ellipse2 before the rotation:

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)

This results in the error "AttributeError: 'QGraphicsEllipseItem' object has no attribute 'setTransformOriginPoint'

Obviously, I'm doing something wrong or making an incorrect assumption about QGraphicsEllipseItem. Some sites hint that I may need to use a bounding rectangle in order to do the rotation, but I don't understand how to do that.

If someone could show me the correct way to rotate an ellipse about its center in pyqt, I would greatly appreciate it!!!

Это было полезно?

Решение

Ok, so after many weeks I was able to find my own answer although I don't really really understand why it works. My standard method of programming by brail. Anyway, the code should look like this:

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)

So this successfully places the center of the rotated ellipse at the point 500,500. I'm not sure why you would take the negative of the angle you want to rotate, but it seems to work. If anyone can explain why it works this, I would appreciate it.

Другие советы

I got the same problem and spent two whole days to solve it.
This is my solution:
First of all you should define the coordinates(x,y) of the point around which the ellipse should rotate, this way:
ellipse.setTransformOriginPoint(QPointF(?, ?))
then you can use the setRotation() to rotate ellipse
the whole code can be seen below:

__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_()

Pay attention to the way I've calculated the center of the ellipse.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top