Qt World Summit: Submit your Presentation

Scrolling the contents of a GraphicsView

  • Hello everyone,

    I have a few questions regarding the GraphicsView behavior. I am displaying an image that is in a scene as usual.

    First I want to know if I'm right thinking this:
    Basically I am doing QGraphicsView::scale(x, x), setting QGraphicsView::NoAnchor, so if x is 2 for example, the scene is twice as big in both directions, but the top left point is the same, so my viewport is displaying the top left area which is in this case 1/4 of the new scaled area…is this correct or do I have it all wrong??

    Then when I scale my view, I want to be able to control what area of the image I show in the viewport, so how can I scroll the scene to specific values to fit in my viewport exactly the area of the image I want to display??
    To scroll the scene I tried doing:


    Assuming I calculated the values dx and dy to specify what to show in the viewport, but it's not scrolling anything..

  • I've tried a few things and I think if I get the following right, things should work as I want them to.
    So what I'm doing is:

    1. I draw a rubberband rectangle on my image. I calculate the center point of that rectangle and save those coordinates.
    2. Then I do a scale transform to the view to fit the rubberband rectangle I selected previously to fit in the whole viewport.
    3. then I use the function:
      QGraphicsView::centerON(centerPoint) // centerPoint being the coordinates I saved in #1.

    Now my question is, I think the coordinates I saved in #1 through the mouse events (i.e. event->pos()) in press and release events are in View coordinates. So if I map those to scene coordinates with:

    QGraphicsView::mapToScene(x, y);

    and save that point, then after the scaling transformation doing centerOn(centerPoint) should center the viewport in the exact same point of the scene, just now the scene is scaled but the center point is the same..Is this logic right? For now I don't think its centering the viewport where I want it to so I think somewhere I'm missing something.

    Could someone please help me get on the right track??

Log in to reply