I have the following problem that I really can't find the solution. I have a QGraphicsItem, which has a size of 2000m X 2000m scaled on the screen. At a certain moment, I zoom, with which whatever is drawn in that QGraphicsItem looks bigger... now, how do I know at a certain moment, after zooming several times, how many meters wide and I'm seeing how high???...because if I ask the QGraphicsItem it always tells me 2000x2000...but having zoomed in, I'm seeing fewer meters on the screen. I can't put the real code because it has a lot of other things, but I'm going to put an approximation.
GridFarmGraph *grid = new GridFarmGraph(view->getWidth(), view->getHeight());
FarmGraph *farmGraph = new FarmGraph(2000);
farmGraph->setTransform(QTransform().scale(view->getWidth() / 2000, view->getHeight() / 2000);
farmGraph->setParentItem(grid);
GridFarmGraph is a graphicsitem that is below the size of the View. FarmGraph is a graphicsitem that is a child of GridFarmGraph and it is scaled so that on the screen it can simulate 2000m wide by 2000m high...so far so good...if I click with the mouse it really tells me where in those 2000x2000 clickie !. But when I zoom in, obviously on the screen, when everything gets bigger, I see less than 2000 x 2000 meters... what I need is to know how much I'm seeing.
The class
QGraphicsItem
has a couple of methods (mapFromScene
andmapToScene
) that let you know the relationship between the original (unscaled) stage and your current scale.For example, if we want to know the surface that is actually displayed, we can take the coordinates of the corners of the widget:
If, instead, you need to know the actual coordinates of a point selected by the user: