Scale items position relative to the scene/view resizing

  • I'm developing a software using Qt. I have a QGraphicsView that can be resized.

    I want to let users load an image (some maps) as the background of the view and be able to place some markers on the image (QGraphicsPixmapItem) and move them around. The positions of the markers on the image are important to be kept since they are markers on the map.

    If users try to resize the view by resizing the window, the image will be also resized. So the markers will not be at the previous positions of the map.

    It seems that the markers positions should also be scaled to the view.

    So my question is how to fix items position on the map even while resizing?

  • well, that's going to involve some math. Yes, you need to scale the x,y locations percentage based on the new image percent increase or decrease..

    For the map visual to remain valid you must keep the same aspect ratio, so essentially it's just a scale factor. Multiply the scale factor by the x and y components of each marker location to get the new location.

  • Call fitInView on the GraphicsView with the Image whenever the size of the GraphicsView changes. The rest should work automatically.