Search code examples
qtqt5qgraphicsviewqgraphicssceneqgraphicsitem

QGraphicsItem: how to move it automatically with when QGraphicsScene resizes?


I have a QGraphicsScene and a few QGraphicsItem objects on it. It is a data flow diagram.

I want to tie a few objects (input and output ports) to scene borders, and when scene size changes, they should automatically move with the scene bord

enter image description here Cannot you tell me, how can it be done?


Solution

  • Bind a method in the QGraphicsItem implementation to the sceneRectChanged signal of the scene object, and have that method adjust it's object's position to take into account the new scene rect.

    Depending on what you are trying to do, you might need to keep a copy of the previous scene rect for reference so you can work out what the change is.