Search code examples
qtqgraphicsview

graphicsView scale ignoring in drawbackground


I just drawn grid lines in the QGraphicsView on its background

  drawbackground(QPainter *painter, const QRectF &rect)
    {
    QVarLengthArray<QLineF, 100> linesX;
    for (qreal x = left; x < sceneRect.right(); x += gridInterval )
    {

    linesX.append(QLineF(x, sceneRect.top(), x, sceneRect.bottom()));
    }

    QVarLengthArray<QLineF, 100> linesY;
    for (qreal y = top; y < sceneRect.bottom(); y += gridInterval ){

    linesY.append(QLineF(sceneRect.left(), y, sceneRect.right(), y));
    }

    painter->drawLines(linesX.data(), linesX.size());
    painter->drawLines(linesY.data(), linesY.size());
    }

and im scaling the view by

void
ViewPort::zoomIn()
{
    zoom(qreal(1.2));
}

void
ViewPort::zoomOut()
{
    zoom(1 / qreal(1.2));
}

void
ViewPort::zoom(qreal scaleFactor)
{

    qreal factor = transform().scale(scaleFactor, scaleFactor).mapRect(QRectF(0, 0, 1, 1)).width();

    if (factor < 1 || factor > 100)
        return;

    scale(scaleFactor, scaleFactor);

}

now the scen has plenty of items that i have to scale but i need to ignore the grid lines i have drawn on the graphicsView drawBackground . how i can ignore the scale transformation of gridLines.?

i tried QPainter::resetTransform() but in vain ..


Solution

  • If you aggregate the lines into an object which you derive from QGraphicsItem and add the item to the scene, you can then set the flag QGraphicsItem::ItemIgnoresTransformations, to stop it responding to scaling and other transformations.