GraphicsView Zoomout scale Issue

106 Views Asked by At

My work Environment : Qt 5.8 MSVC2015 64bit, QT GraphicsView, Windows 7 64 bit

When GraphicsView vertical scroll bar is goes away, zoom out should stop.

So I tried with below code, but it failed to work :

void GraphicsView::scale(qreal scaleFactor)
{
    QRectF r(0, 0, 1, 1); // A reference
    int pos_x = this->horizontalScrollBar()->value();
    int pos_y = this->verticalScrollBar()->value();

    qreal factor = transform().scale(scaleFactor, scaleFactor).mapRect(r).width(); // absolute zoom factor


    if ( factor > 7) { // Check zoom out limit
        return;
    }

   //Failed, this code failed If zoom out again.** 
   if(pos_x <= 0 && pos_y <= 0 ) 
    {
        return;
    }

Any suggestion How I can do to fix the above code ?

1

There are 1 best solutions below

0
On

No reply for my question. Here is work around solution from me, Check from wheelEvent are we doing zoom in or zoom out. I scale check vertical & horizontal scroll bar.

here _steps is private data member of my class GraphicsView. GraphicsView derived from QGraphicsView.

void GraphicsView::wheelEvent(QWheelEvent * event)
{
    // Typical Calculations (Ref Qt Doc)
    const int degrees = event->delta() / 8;
    _steps = degrees / 15;  // _steps = 1 for Zoom in, _steps = -1 for Zoom out.

}



void GraphicsView::scale(qreal scaleFactor)
{
    QRectF r(0, 0, 1, 1); // A reference
    int pos_x = this->horizontalScrollBar()->value();
    int pos_y = this->verticalScrollBar()->value();
    qreal factor = transform().scale(scaleFactor, scaleFactor).mapRect(r).width(); // absolute zoom factor
    if ( factor > 7) { // Calculate your zoom in limit from factor
        return;
    }

 //When there is no scroll bar, am I still I am zooming, stop it using _steps  
    if(pos_x <= 0 && pos_y <= 0 && _steps == -1)
    {
        return;
    }
    QGraphicsView::scale(scaleFactor, scaleFactor);
}

I know there is better solution then this, But I found this only :(