doctor joe
doctor joe

Reputation: 65

Qt - zoom in/out with QSlider

I want to integrate a zoom slider for a QGraphicsView. I use QGraphicsView::scale() for zoomig.

Here is my code:

    void MainWindow::on_sld_zoom_valueChanged(int value)
{
    ui->graphicsView->setTransformationAnchor(QGraphicsView::AnchorViewCenter);
    double scaleFactor;
    scaleFactor = pow(1.1,((value - 100) / 100.0));
    ui->graphicsView->scale(scaleFactor,scaleFactor);
}

My min slider value is 1, max is 200 and when value is 100, my scaleFactor is 1 according to my function.But if scaleFactor is bigger than 1, slider always zooming in. For example, when I change value from 150 to 149, it should be zoom out but it doesn't because zoom factor is bigger than 1.

How can I solve this problem?

Upvotes: 0

Views: 1483

Answers (1)

mvidelgauz
mvidelgauz

Reputation: 2214

ui->graphicsView->scale() is relative action. Below is my on_ZoomSliderValueChanged(int value) that scales QGraphicsView to acording to current slider position. Hope it will help you (you will probably want to recalculate newScale according to your desired curve):

void PictureWindow::on_ZoomSliderValueChanged(int value)
{
    qreal newScale = qPow(m_pPimpl->m_ZoomFactor, value);

    QMatrix matrix;
    matrix.scale(newScale, newScale);

    ui->graphicsView->setResizeAnchor(QGraphicsView::ViewportAnchor(m_pPimpl->m_ViewportAnchor));
    ui->graphicsView->setMatrix(matrix);
}

Upvotes: 1

Related Questions