[SOLVED]Get scene coordinates relative to viewport.



  • I have a QGraphicsView that contains a QGraphicsScene. The scene is not entirely visible as it is too big so I have to move it around in order to see parts of the image contained in the scene that are not visible.

    Ideally what I want to achieve is to have coordinates for two rectangles.
    One of them is the rectangle that defines the QGraphicsView's viewport and the other one is a rectangle that will tell me where the scene is relative to the viewport.

    Thus both rectangles will use the same coordinate system as a reference and I can find interesting values like how much of the scene is visible etc.

    Can this be done?



  • QGraphicsView provides a set of mapFromScene and mapToScene functions for such purpose.



  • I am aware of those function of curse, but I did not reach what I was hoping for.

    something like

    QRect viewportRect(0, 0, view->viewport()->width(), view->viewport()->height());
    QRectF visibleSceneRect = view->mapToScene(viewportRect).boundingRect();
    

    Or any of the many combinations that I tried did not produce the result I wanted.

    When I drag the image in display for example and you can no longer see its top left corner I would expect to get negative values for the x and y of the rectangle that represents the image, lets call them x1, y1. Those values should get increasingly smaller as I keep moving to the bottom right corner of the image and my visible area gets further from x1, y1.
    Yet all the things I have tried don't really care how I move the image around (or in other words, what part of the image is currently visible).



  • QGraphicsScene::itemsBoundingRect() will return a rectangle over all items in the scene, in scene coordinates.
    Using QGraphicsView::mapFromScene, you can map it to view coordinates. Now you can compare that result to the width and height of your actual viewport. Negative coordinates are outside the view area on left and top.



  • Hi Asperamanca,

    Trying your suggestion I could not observe anything different from what I described above. If I scroll (remember that my picture is bigger than my viewport) then the values you suggest do not change.



  • QGraphicsScene::itemsBoundingRect() will always return the same values, unless you change something in the scene itself.

    But QGraphicsView::mapFromScene should take your current scroll position into account. At least it does for me.



  • What I did was

    view-> mapFromScene(view->scene()->itemsBoundingRect());

    But no values ever changed.



  • Can you post a specific example and some values? Might be easier to see then.



  • I am probably tired but I did another test with your solution and it works. My update function had the problem all along, and even though what you said produced the results they only got printed during a resize event.

    Thanks for the help and sorry for not properly testing your suggestion.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.