0

我的工作环境:Qt 5.8 MSVC2015 64bit, QT GraphicsView, Windows 7 64 bit

当 GraphicsView 垂直滚动条消失时,缩小应该停止。

所以我尝试了下面的代码,但它失败了:

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;
    }

任何建议我该怎么做才能修复上述代码?

4

1 回答 1

0

没有回答我的问题。这是我的解决方案,从 wheelEvent 检查我们是在放大还是缩小。我缩放检查垂直和水平滚动条。

这里 _steps 是我的类 GraphicsView 的私有数据成员。GraphicsView 派生自 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);
}

我知道有比这个更好的解决方案,但我发现只有这个:(

于 2017-05-30T07:54:08.097 回答