I noticed behavior that may be a bug, but would like feedback before posting it in the Qt bug tracker as I may be misunderstanding the intended behavior.

The issue is getting different results when rotating a QGraphicsItem using the .setRotation convenience function versus the .setTransform method and passing a QTransform. The QTransform method appears to not respect the transform origin point as set with QGraphicsItem.setTransformOriginPoint.

Here is a minimal working example that demonstrates the different behavior.

from PySide6 import QtGui
from PySide6.QtWidgets import (
    QApplication,
    QGraphicsRectItem,
    QGraphicsScene,
    QGraphicsView,
    QMainWindow,
    QPushButton,
    QVBoxLayout,
    QWidget,
)


class MyWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.layout = QVBoxLayout()
        self.central_widget = QWidget(self)
        self.setCentralWidget(self.central_widget)
        self.central_widget.setLayout(self.layout)

        self.view = QGraphicsView()
        self.layout.addWidget(self.view)
        self.scene = QGraphicsScene(self)
        self.view.setScene(self.scene)

        self.rectangle = QGraphicsRectItem(0, 0, 500, 275)
        pen = QtGui.QPen()
        pen.setWidth(25)
        pen.setColor("Yellow")
        self.rectangle.setPen(pen)

        self.scene.addItem(self.rectangle)

        self.button_with_QTransform = QPushButton(
            text="Rotate Using QTransform",
            parent=self,
        )
        self.button_without_QTransform = QPushButton(
            "Rotate without QTransform",
            self,
        )
        self.layout.addWidget(self.button_with_QTransform)
        self.layout.addWidget(self.button_without_QTransform)
        self.button_with_QTransform.clicked.connect(
            self.transform_rectangle_with_QTransform
        )
        self.button_without_QTransform.clicked.connect(
            self.transform_rectangle_without_QTransform
        )
        self.rectangle.setTransformOriginPoint(self.rectangle.boundingRect().center())
        self.current_rotation = 0

    def transform_rectangle_with_QTransform(self):
        self.current_rotation += 10
        transform = QtGui.QTransform()
        transform.rotate(self.current_rotation)
        self.rectangle.setTransform(transform)

    def transform_rectangle_without_QTransform(self):
        self.current_rotation += 10
        self.rectangle.setRotation(self.current_rotation)


def main():
    app = QApplication()
    window = MyWindow()
    window.show()
    window.showMaximized()
    app.exec()


if __name__ == "__main__":
    main()
0

There are 0 answers