2
votes

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.setWindowFlags(Qt::FramelessWindowHint);
    view.setGeometry(0, 0, monitorWidth, monitorHeight);
    view.showFullScreen();

    QGraphicsScene scene;
    scene.setBackgroundBrush(Qt::black);
    scene.setSceneRect(QRectF(0.f, 0.f, monitorWidth, monitorHeight));
    view.setScene(&scene);

    //Create 4 rectangle items
    QGraphicsRectItem items[4];

    //top left corner
    items[0].setRect(QRect(0, 0, rectDim, rectDim));
    items[0].setPen(QPen(Qt::red));
    items[0].setBrush(Qt::red);

    //top right corner
    items[1].setRect(QRect(monitorWidth-rectDim, 0, rectDim, rectDim));
    items[1].setPen(QPen(Qt::red));
    items[1].setBrush(Qt::red);

    //bottom left corner
    items[2].setRect(QRect(0, monitorHeight-rectDim, rectDim, rectDim));
    items[2].setPen(QPen(Qt::red));
    items[2].setBrush(Qt::red);

    //bottom right corner
    items[3].setRect(QRect(monitorWidth-rectDim, monitorHeight-rectDim, rectDim, rectDim));
    items[3].setPen(QPen(Qt::red));
    items[3].setBrush(Qt::red);

    for(unsigned int i=0; i<4; ++i)
    {
        scene.addItem(items+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:

view.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
view.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

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 reference, I am unable to figure out what is going wrong. Please help.

1
Does it work correctly if you call view.fitInView(scene.sceneRect())?TheDarkKnight
Yes, that would work. That is because fitInView scales the scene (by manipulating the view matrix) to fit within View. But that isn't what I am looking for. A scene of any dimensions can be made to fit inside a QGraphicsView that way. For my application, I cannot allow such scaling. I want a 1 to 1 mapping between pixels on screen and pixels in QGraphicsScene.balajeerc
Yes, I know how it works, but was going to ask you other questions depending on your answer. Anyhow, I see you've solved it, so that's great.TheDarkKnight

1 Answers

1
votes

Got the answer from a reply to a cross post I made on Qt forums.

The mismatch in coordinates arises from the extra pixels that the frame around QGraphicsView introduces. It is solved by setting:

view.setFrameShape(QGraphicsView::NoFrame);