Time for action – implementing the ability to scale the scene

Let's do the scaling first. We add the item to a scene and put that scene on a custom view we have subclassed from QGraphicsView. In our customized view, we only need to reimplement wheelEvent() as we want to scale the view by using the mouse's scroll wheel.

void MyView::wheelEvent(QWheelEvent *event) {
  const qreal factor = 1.1;
  if (event->angleDelta().y() > 0)
    scale(factor, factor);
  else
    scale(1/factor, 1/factor);
}

What just happened?

The factor parameter for the zooming can be freely defined. You can also create a getter and setter method for it. For us, 1.1 will do the work. With event->angleDelta(), you get the distance of the mouse's wheel rotation as a QPoint pointer. Since we ...

Get Game Programming Using Qt now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.