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.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":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:
@
view.setFrameShape(QGraphicsView::NoFrame);
@ -
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.
-
- QGraphicsView is subclass of QAbstractScrollArea, which is subclass of QFrame.
- QGraphicsScene is shown on the viewPort() of QAbstractScrollArea
- Frame is between the viewPort() and QAbstractScrollArea