Important: Please read the Qt Code of Conduct -

QGraphicsView and item moving issue

  • When I have:

    1. QGraphicsView with a QGraphicsScene (without sceneRect() set)
    2. Add two QGraphicsObjects inside the QGraphicsScene (in this moment, the size of the scene is still smaller then the QGraphicsView viewport).
    3. When I move one Object the scene is resized and re-centered, but this causes the impression that both objects are being moved.

    Is there any way to avoid this ? Thanks !

  • sceneRect : QRectF
    This property holds the scene rectangle; the bounding rectangle of the scene.

    The scene rectangle defines the extent of the scene. It is primarily used by QGraphicsView to determine the view's default scrollable area, and by QGraphicsScene to manage item indexing.

    If unset, or if set to a null QRectF, sceneRect() will return the largest bounding rect of all items on the scene since the scene was created (i.e., a rectangle that grows when items are added to or moved in the scene, but never shrinks).

    Access functions:

    QRectF sceneRect () const
    void setSceneRect ( const QRectF & rect )
    void setSceneRect ( qreal x, qreal y, qreal w, qreal h )

    this is the normal behavior ... set it in order to avoid this

  • Ok, but when I set the sceneRect(), the QGraphicsView doesn't adjusts the scene automatically when someone drags an object to the corner of the viewport. It seems that there is no way to have logic that automatically adjusts the scene size and also a behavior where the objects doesn't seems to move apart when someone moves them.

  • I think you could: the visible growing effect occur until the scene rect will get greater or equal with the view's rect; so you need first to add to scene some dummy items(or move forth and back one of your objects) in all four directions just a little over the view's edges and keep scene rect (and the view's one ) unset to get it growing automatically and view adjusting its scrolling area as well

Log in to reply