Important: Please read the Qt Code of Conduct -

Resizing and moving a QGraphicsItem

  • Hello,

    I have a QGraphicsPathItem that I create after 'drawing' with the mouse on a scene. I have a background image in the scene and this path item gets created on top of the image which is almost transparent reflects a selection of an area of the underlying image. So basically I have my scene with two items.

    Now when I press the zoom button I don't actually do any transformations to my scene or view, I simply compute the appropriate image somewhere else and then replace the image to the zoomed one. But now I need the drawn image also to take into account the zoom level and when I scroll to also scroll accordingly. I was trying to do this getting the boundingRect of the item and calculate a new bounding rect according to zoom and scroll position and then setting that calculated rectangle to be the new bounding rectangle of my item, so my item would resize itself and move to the right position whether I zoom or scroll. But this is not the right way since it gives very strange behavior. I do all the prepareForGeometryChange() before setting the bounding rectangle which I set in a function like this:

    QRectF PathItem::boundingRect() const
    return m_rect;

    void PathItem::setBoundingRect(QRectF rect)
    m_rect = rect;

    What would be the right way to do this? Basically how can I have an item in the scene move and rescale itself to a pre-calculated rectangle I provide it? Any hints and tips greatly appreciated.


Log in to reply