【问题标题】:GraphicsView Zoomout scale IssueGraphicsView 缩小比例问题
【发布时间】:2017-10-29 20:16:10
【问题描述】:

我的工作环境: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;
    }

任何建议我可以如何修复上述代码?

【问题讨论】:

    标签: c++ qt qt5.5 qt5.7


    【解决方案1】:

    没有回答我的问题。这是我的解决方案, 从 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);
    }
    

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

    【讨论】:

      猜你喜欢
      • 2021-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多