I want to implement grid in my graphicsView such that it fits to the graphicsView automatically and when I zoom in the graphicsView only the block size of grid should increase but not the line width of the grid. I tried the following but nothing happened.
void CadGraphicsScene::grid(QPainter *painter, const QRectF &rect)
{
QPen pen;
painter->setPen(pen);
qreal left = int(rect.left()) - (int(rect.left()) % gridSize);
qreal top = int(rect.top()) - (int(rect.top()) % gridSize);
QVector<QPointF> points;
for (qreal x = left; x < rect.right(); x += gridSize){
for (qreal y = top; y < rect.bottom(); y += gridSize){
points.append(QPointF(x,y));
}
}
painter->drawPoints(points.data(), points.size());
}
Please help me out to make a grid.
1) Use cosmetic pen (with zero width) 2) By QT idiom graphics scene are independent from view (about your question of zoom in graphicsview), but you can extract zoom coefficients of view from passed QPainter object (QPainter *painter) - QPainter::worldTransform -> QTransform::m11 (horz_Scale) & QTransform::m22 (vert_Scale) - in this case you can recalculate grid anchors (for 100% zoom QTransform::m11 == QTransform::m22 == 1.) on 'fly'