Qt World Summit: Submit your Presentation

Understanding the Mapping from View Coordinates to Scene Coordinates

  • I was expecting the following snippet of code to show me 4 squares on each corner of my monitor (whose resolution is 1920x1080) on a frameless and fullscreen QGraphicsView that occupies the entire monitor's framebuffer:

    @int main(int argc, char **args)
    QApplication app(argc, args);

    const int monitorWidth = 1920;
    const int monitorHeight = 1080;
    const int rectDim = 40;

    QGraphicsView view;
    view.setGeometry(0, 0, monitorWidth, monitorHeight);
    QGraphicsScene scene;
    scene.setSceneRect(QRectF(0.f, 0.f, monitorWidth, monitorHeight));
    //Create 4 rectangle items
    QGraphicsRectItem items[4];
    //top left corner
    items[0].setRect(QRect(0, 0, rectDim, rectDim));
    //top right corner
    items[1].setRect(QRect(monitorWidth-rectDim, 0, rectDim, rectDim));
    //bottom left corner
    items[2].setRect(QRect(0, monitorHeight-rectDim, rectDim, rectDim));
    //bottom right corner
    items[3].setRect(QRect(monitorWidth-rectDim, monitorHeight-rectDim, rectDim, rectDim));
    for(unsigned int i=0; i<4; ++i)
    return app.exec();

    } @

    I find that I get scroll bars in my QGraphicsView which appear when the QGraphicsScene Rect is out of bounds of the QGraphicsView viewport. However, as shown in the code above, both match exactly. Even if I hide the scroll bars using:


    I find that the bottom and right squares are clipped incompletely meaning that there is still some part of QGraphicsScene which is not visible in the QGraphicsView. Why is this the case?

    Clearly, my confusion is in the mapping of scene coordinates to that in view. Despite reading the information on "coordinate systems in the QGraphicsView":http://qt-project.org/doc/qt-4.8/graphicsview.html#the-graphics-view-coordinate-system reference, I am unable to figure out what is going wrong. Please help.

  • Hi, welcome to Qt forums,

    Seems what you are looking for is:

  • Yes, that seems to fix the issue, thanks. That also explains a 2 pixel difference observed between the view coords and the scene coords which if I set as below:

    @scene.setSceneRect(QRectF(0.f, 0.f, monitorWidth-2, monitorHeight-2));@

    the toolbars and the clipping disappeared even with the old code.

    1. QGraphicsView is subclass of QAbstractScrollArea, which is subclass of QFrame.
    2. QGraphicsScene is shown on the viewPort() of QAbstractScrollArea
    3. Frame is between the viewPort() and QAbstractScrollArea

Log in to reply