Relationship between QtGraphicsScene and QtGraphicsView Geometry

  • I am not able to relate the geometry of QGraphicsView,QGraphicsScene and QGraphicsRectItem

    Below is the code
    QGraphicsScene *scene=new QGraphicsScene(QRectF(-50,-50,400,400));

    QGraphicsRectItem *rectItem = new QGraphicsRectItem(-50,50,100,100);

    QGraphicsView *view = new QGraphicsView(scene);



    If i m not setting geometry of view,then i am able to relate

  • Hi,

    I'm not sure what you mean by "to relate geometry".

    Anyway, The rect passed to the QGraphicsScene defines kind of the "bounding rect of the scene": . If not set, it is derived from the items in the scene. It is in scene coordinates.

    QGraphicsRectItem uses the given coordinates as upper left corner width and height. And they are in scene-coordinates as well.

    setGeometry of the GraphicsView is actually inherited from "QWidget": and refers to pixels on the screen. It is not in Scene-Coordinates. In other words there is no relation between the former two sets of coordinates and the later set of coordinates.

    Maybe you were aware of all that, as I said I didn't really grasp your question.

    Best Soraltan

  • Actually i am setting view and scene geometry,and now i want to add items in scene at a desired location.

    -50,-50,400,400 -> scene geometry
    -50,-50,100,100 -> item geometry

    0,0,800,800 -> view geometry

    i thought item will not be visible or i have to scroll to see the item but item is visible in the view without scrolling,so little bit confused.

  • Ok, so the view geometry has nothing to do with the other two geometries.

    The view geometry refers to the position of the QWidget (in the end QGraphicsView is just a QWidget like QLabel or QPushButton) within your window. It is in pixel.

    The scene and item geometry are given within the QScene coordinates. When instantiating a QScene this spans a 2D space in which items are placed. These coordinates are floating-points (you could create a QGraphicsRectItem with 0.5 1.5 3.7 4.2 for instance). They have nothing to do with pixels!

    When you create a QSceneView, this is really just a view to the scene you created. You can zoom into and out of the scene and scroll around the scene calling the fitting methods. This leaves your scene totally unaffected. You can find an introduction to the QGraphicsFramework "here":

    I hope I could make clear the difference between the two types of geometry.



  • Thanks Soraltan,

    But still i am not getting.

    what should i set scene geometry to show my item in top left corner of view?

    0,0,800,800 -> view geometry
    0,0,400,400 -> scene geometry
    0,0,100,100 -> item geometry

    In above scenario,my item is not coming in top-left corner of view

  • I do my best :-)

    Anyway having the view show a specific fraction of the scene is something I would like to accomplish, "too":

    You can use the "translate": operation to navigate through the scene. Or use "ensureVisible": to make sure your rect is within the view. But it does nothing more than that apparently. It does not even ensure that the rect is displayed reasonably large, much less that it is in the upper left corner.

    I have not yet found a convenient way to aligning the viewport to some elements within the scene.


    But if you do, pleeeease let me know!!

  • Thanks Soraltan,

    It will be helpful if some one can give the clear idea of co-ordinates of QGraphicsView and QGraphicsScene with some small simple examples

  • The geometry of the QGraphicsView has nothing to do with geometry and content of the scene. It is the position and size of the widget, which then acts like a "window" showing part (or all) of the scene.

    The sceneRect of the QGraphicsScene defines the area within a scene that can be navigated within a view, i.e. it defines how far you can zoom and scroll. If you don't take care of it yourself, it automatically grows to encompass all GraphicsItems.

    Finally, to choose which part of the scene you can see within QGraphicsView, there are multiple methods such as centerOn, ensureVisible and fitInView.
    By default, the sceneRect is used. So in your case, because the sceneRect starts at -50/-50, the view also shows negative coordinates of your scene.

    If you don't want that, use the functions I mentioned above after you set the scene on the view, and move your viewport position where you want it.

  • Thanks
    May be my question is not correct but let us say
    view -> geometry is 0,0,600,600

    scene -> geometry is 0,0,400,400

    so for (0,0) point of view what is corresponding point in scene
    how could i know that

  • Hi
    This example give me a clear idea about geometry.
    Hope it will help to others
    I think by default view is concentrating on the center point of scene

    #include <QApplication>
    #include <QPushButton>

    int main(int argc, char* argv[])

    QApplication app(argc, argv);

    QWidget w;

    QGraphicsScene scene(0,0,600,600);

    scene.addRect(10,10,100,100,QPen(Qt::yellow, Qt::SolidLine));
    scene.addRect(-10,-10,-100,-100,QPen(Qt::blue, Qt::SolidLine));
    scene.addRect(-10,10,-100,100,QPen(Qt::red, Qt::SolidLine));
    scene.addRect(10,-10,100,-100,QPen(Qt::green, Qt::SolidLine));
    scene.addRect(300,300,200,200,QPen(Qt::black, Qt::SolidLine));

    QGraphicsView view(&scene,&w);

    QPushButton *button1 = new QPushButton("hi",&w);




  • Use QGraphicsView::mapToScene / mapFromScene to translate positions.

    @QPointF topLeftVisibleScenePosition = pView->mapToScene(QPoint(0,0));@

  • Thanks,
    Good Suggestion

Log in to reply

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