QGraphicsView shows gap under scene
-
When displaying a scene in a QGraphicsView that complete fits in, there's a small gap between the end of the scene and the end of the view.
!http://i43.tinypic.com/2qss083.jpg!
If I make the views height smaller so that the scene doesn't fit, the gap disappears:
!http://i39.tinypic.com/5cgc1w.jpg!
How do I get rid of this gap?
Here's the code:
@
#include <QtWidgets>void line(QGraphicsScene &scene, int x, int y, QColor color = QColor("#9BA300"))
{
QGraphicsRectItem *item = scene.addRect(0, 0, 2, y, QPen(Qt::NoPen), QBrush(color));
item->setPos(3 * x, 0);
}int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QGraphicsView graphicsView;
graphicsView.resize(320, 240);
graphicsView.show();
graphicsView.setWindowTitle(QApplication::translate("GraphicsSandbox", "Graphics sandbox"));graphicsView.setFrameStyle(QFrame::NoFrame);
graphicsView.setRenderHint(QPainter::Antialiasing, true);
graphicsView.setDragMode(QGraphicsView::NoDrag);
graphicsView.setOptimizationFlags(QGraphicsView::DontSavePainterState);
graphicsView.setViewportUpdateMode(QGraphicsView::SmartViewportUpdate);
graphicsView.setAlignment(Qt::AlignBottom | Qt::AlignLeft);
graphicsView.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
graphicsView.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
graphicsView.setTransformationAnchor(QGraphicsView::NoAnchor);QGraphicsScene scene;
scene.setItemIndexMethod(QGraphicsScene::NoIndex);
scene.setBackgroundBrush(QBrush("#242082"));
graphicsView.setScene(&scene);QMatrix matrix;
matrix.scale(1, -240/40);
graphicsView.setMatrix(matrix);for (int i = 0; i < 20; ++i)
{
line(scene, i, 2 * i);
}return app.exec();
}
@ -
what does the line method do?
-
The function line is at the top of my code. It adds a rect and set the position. Basically it draws the green bars.
-
i know ;)
But i wanted to see the code to check for errors there (e.g. for rounding errors etc.) -
Can you simplify the example so that we can easily see the actual coordinates of the line that you draw, and compare it to the size of the scene?
I believe this has to do with the way that coordinates are translated into actual pixels (turning off antialiasing might make that effect more visible).
Basically, an integer coordinate is not exactly "a pixel", it is at the point between four pixels. Coordinates then have to be mapped to the pixels, i.e. the painter has to decide which pixels to fill, and which not to fill.
Depending on your exact values, the visible result might not be what you expect. -
(I thought I already post this :?)
@int main(int argc, char *argv[])
{
QApplication app(argc, argv);QGraphicsView graphicsView;
graphicsView.resize(160, 120);
graphicsView.show();
graphicsView.setAlignment(Qt::AlignBottom | Qt::AlignLeft);QGraphicsScene scene;
graphicsView.setScene(&scene);
scene.addRect(0, 0, 100, 100, QPen(Qt::NoPen), QBrush(QColor("black")));return app.exec();
}@With gap on the bottom (not on the left!)
!http://i41.tinypic.com/2w6w9yu.jpg!Without gap (the scene is smaller in height) than the view, so obviously no gap!)
!http://i42.tinypic.com/177bjn.jpg! -
True, there are some posts missing.
-
qt-project.org servers had some troubles and it may be that data got lost.