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 only care about vertical scrolling, just the y axis is relevant for us. In our example, we also do not care about how far the wheel was turned because, normally, every step is delivered separately to wheelEvent(). But if you...