title: Qt 5 : Mouse Wheel event behaviour for zooming image
body: <p>I have a window, in which is a QGraphicsView, which will be displaying an image. I have implemented the wheelEvent(). My images are mostly going to be bigger than the window, so I get scroll bars in the window.</p>
  
  <p>What we normally observe when we rotate the wheel while viewing an image in Windows Photo Viewer is that when we move the wheel up (towards it's wire), the image zooms in and when we move it down (towards out body), the image zooms out.</p>
  
  <p>What I am getting instead is when I move the wheel towards myself (to zoom out) the image instead of zooming out , first scrolls down, and starts zooming out only when the scroll bar touches its bottom most point.</p>
  
  <p><strong>It would be better to understand the problem by trying out the code. I am not able to explain in I guess.</strong></p>
  
  <p>I want the standard behavior. What to do?</p>
  
  <p><strong>Code</strong></p>
  
  <pre><code>#include "viewer.h"
  #include "ui_viewer.h"
  #include &lt;QGraphicsView&gt;
  #include &lt;QGraphicsItem&gt;
  #include &lt;QGraphicsPixmapItem&gt;
  #include &lt;QWheelEvent&gt;
  #include &lt;QDebug&gt;
  #include &lt;QImage&gt;
  #include &lt;QImageReader&gt;
  #include &lt;QApplication&gt;
  #include &lt;QDesktopWidget&gt;
  
  viewer::viewer(QWidget *parent) : QWidget(parent),ui2(new Ui::viewer)
  {
       ui2-&gt;setupUi(this);
  } 
  
  viewer::~viewer()
  {
      delete ui2;
  }
  
  int viewer::show_changes(QString folder)
  {
      QDesktopWidget *desktop = QApplication::desktop();
      int screenWidth  = desktop-&gt;width();
      int screenHeight = desktop-&gt;height();
      QString filename = "image_bigger_than_window.jpg";
      QPixmap pixmap = QPixmap(filename);
      QImageReader reader(filename);
      QImage image = reader.read();
      QSize size = image.size();
      int width = 800;
      int height = (width * size.height()) / size.width();
      int x = (screenWidth - width) / 2;
      int y = (screenHeight - height) / 2 - 30;
      setGeometry(x,y,width, height);
      setWindowTitle("OUTPUT");
      ui2-&gt;graphicsView-&gt;setGeometry(0,0,width,height);
      QGraphicsScene* viewScene = new QGraphicsScene(QRectF(0, 0,width,  height), 0);
      QGraphicsPixmapItem *item = viewScene-&gt;addPixmap(pixmap.scaled(QSize((int)viewScene-&gt;width(), (int)viewScene-&gt;height()),
                               Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
      viewScene-&gt;addItem(item);
      ui2-&gt;graphicsView-&gt;fitInView(QRectF(0, 0, width,     height),Qt::IgnoreAspectRatio);
      ui2-&gt;graphicsView-&gt;setScene(viewScene);
      ui2-&gt;graphicsView-&gt;show();
      return 0;
  }
  
  void viewer::wheelEvent(QWheelEvent * event)
  {
      const int degrees = event-&gt;delta() / 8;
      qDebug() &lt;&lt; degrees;
      int steps = degrees / 15;        
      double scaleFactor = 1.0; 
      const qreal minFactor = 1.0;
      const qreal maxFactor = 10.0;
      qreal h11 = 1.0, h22 = 0;
      if(steps &gt; 0)
      {
          h11 = (h11 &gt;= maxFactor) ? h11 : (h11 + scaleFactor);
          h22 = (h22 &gt;= maxFactor) ? h22 : (h22 + scaleFactor);
      }
      else
      {
          h11 = (h11 &lt;= minFactor) ? minFactor : (h11 - scaleFactor);
          h22 = (h22 &lt;= minFactor) ? minFactor : (h22 - scaleFactor);
      }
      ui2-&gt;graphicsView-&gt;setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
      ui2-&gt;graphicsView-&gt;setTransform(QTransform(h11, 0, 0,0, h22, 0, 0,0,1));
  
  }
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>1)Removed function <code>void viewer::wheelEvent(QWheelEvent * event)</code> from <code>viewer.cpp</code> </p>
  
  <p>2)Put <code>bool viewer::eventFilter(QObject *obj, QEvent *event)</code> in its place as a protected function and <code>void viewer::handleWheelOnGraphicsScene(QGraphicsSceneWheelEvent* scrollevent)</code> as a public slot in <code>viewer.h</code></p>
  
  <pre><code>bool viewer::eventFilter(QObject *obj, QEvent *event)
  {
      if (event-&gt;type() == QEvent::GraphicsSceneWheel)
      {    
          QGraphicsSceneWheelEvent *scrollevent = static_cast&lt;QGraphicsSceneWheelEvent *&gt;(event);
          handleWheelOnGraphicsScene(scrollevent);
          return true;
      }
    // Other events should propagate - what do you mean by propagate here?
    return false;
  }
  
   void viewer::handleWheelOnGraphicsScene(QGraphicsSceneWheelEvent* scrollevent)
  {
     const int degrees = scrollevent-&gt;delta()  / 8;
     qDebug() &lt;&lt; degrees;
     int steps = degrees / 15;
     qDebug() &lt;&lt; steps;
     double scaleFactor = 1.0; //How fast we zoom
     const qreal minFactor = 1.0;
     const qreal maxFactor = 10.0;
     if(steps &gt; 0)
     {
        h11 = (h11 &gt;= maxFactor) ? h11 : (h11 + scaleFactor);
        h22 = (h22 &gt;= maxFactor) ? h22 : (h22 + scaleFactor);
     }
     else
    {
        h11 = (h11 &lt;= minFactor) ? minFactor : (h11 - scaleFactor);
        h22 = (h22 &lt;= minFactor) ? minFactor : (h22 - scaleFactor);
    } 
  ui2-&gt;graphicsView-&gt;setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
  ui2-&gt;graphicsView-&gt;setTransform(QTransform(h11, 0, 0,0, h22, 0, 0,0,1));
  
  }
  </code></pre>