Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Set rotation of QGraphicsItem using the position of its child



  • I have a QGraphicsItem with a child - a small circle implemented as a QGraphicsEllipseItem - which I want to let the user drag around to set the rotation of the parent:

    0_1560175113600_Screenshot_20190610_155812.png
    (thin box on the left is the parent, red circle on right is the child)
    Ideally, the left object should rotate to follow the center of the right circle. I've set up the circle's itemChange event to grab the ItemPositionChange type and then emit a signal with the updated position, which is caught by the parent and used to calculate the parent's rotation:

    self.setRotation(math.degrees(math.atan2(newpos.y(), newpos.x())))
    

    (using Qt for Python, but this question probably doesn't depend on the choice of language)

    The problem I have is that by setting the parent's angle to the vector pointing towards the child, I implicitly update the child's coordinate system as well, so the child moves again in addition to the amount the cursor moved it:

    0_1560176363600_Screenshot_20190610_161810.png
    (the arrow shows the cursor drag path from the initial click)

    Does anyone have a good idea how to avoid this unwanted effect? I guess the obvious answer is to avoid making the red circle a child of the parent so that it doesn't use its coordinate system, but having the circle be a child of the other object makes sense for other reasons (it only exists because of the presence of the other object, etc.) so ideally I'd like to keep it.


Log in to reply