1

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.

For example check out this image. There are 4 markers on the map: View in small size

After making the view full-screen (without changing items position): View in full-screen size

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

Mosi
  • 1,178
  • 2
  • 12
  • 30
  • Good luck. What is your question? – Lior Kogan Jul 11 '19 at 19:50
  • I edited! I don't know how to keep items positions on the map – Mosi Jul 11 '19 at 19:53
  • @Mosi provide a [mre] – eyllanesc Jul 11 '19 at 19:59
  • It's not a bug to reproduce it. This is the way scene/view of qt works. The items positions are absolute relative to their scene. When my scene/view size are changed, the markers positions are not changing. I just want to scale their positions too – Mosi Jul 11 '19 at 20:02
  • @Mosi The items must also change, so if they do not, it seems to me that a certain part of your code avoids it, for that reason an MRE is necessary, so if it is a bug of part of your code. – eyllanesc Jul 11 '19 at 20:14
  • 1
    You have to map item positions to view screen position before zoom/resize. After zoom/resize you map the view screen position back to item positions. Then you can translate from old item position to new item position to fix your view as intended. [`QGraphicsView::mapFromScene()`](https://doc.qt.io/qt-5/qgraphicsview.html#mapFromScene) and [`QGraphicsView::mapToScene()`](https://doc.qt.io/qt-5/qgraphicsview.html#mapToScene) provide the necessary tools for this. – Scheff's Cat Jul 12 '19 at 05:35
  • I once did something similar which may (or may not) be of help: [SO: Zoom functionality using Qt](https://stackoverflow.com/a/47710623/7478597) – Scheff's Cat Jul 12 '19 at 05:38
  • How do you set/scale the background image? – Dimitry Ernot Jul 12 '19 at 08:18

0 Answers0