简体   繁体   中英

Grid in GraphicsView

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)使用化妆笔(宽度为零)2)通过QT习语,图形场景与视图无关(关于您在graphicsview中的缩放问题),但是您可以从传递的QPainter对象(QPainter * painter)-QPainter中提取视图的缩放系数:: worldTransform-> QTransform :: m11(horz_Scale)和QTransform :: m22(vert_Scale)-在这种情况下,您可以重新计算网格锚(对于100%缩放,QTransform :: m11 == QTransform :: m22 == 1) '飞'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM