How to reset the scale in QGraphicsView?

2020-07-20 02:42发布

问题:

How can I reset the scale of a Graphics View regardless of any previous scale that was applied? Using scale() multiplies the scale I give it to the previous one:

ui->graphicsView->scale(0.1, 0.1);
ui->graphicsView->scale(2, 2);
// the scale factor is (0.2,0.2), NOT (2,2)

This is not what I want, I want to just set the scale to (2,2).

回答1:

I looked into the sources and scale() uses a matrix internally:

void QGraphicsView::scale(qreal sx, qreal sy)
{
    Q_D(QGraphicsView);
    QTransform matrix = d->matrix;
    matrix.scale(sx, sy);
    setTransform(matrix);
}

There is a function to reset the matrix, and calling it before applying the scale works:

view->scale(0.1, 0.1);
view->resetMatrix();
view->scale(2, 2);
// the scale factor is (2,2)