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.
If I make the views height smaller so that the scene doesn’t fit, the gap disappears:
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();
}
↧