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.