Smooth Zoom In QGraphicsView

From Qt Wiki
Revision as of 17:47, 14 January 2015 by Maintenance script (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

English 日本語

Smooth Zoom In QGraphicsView

A nice feature to a program with QGraphicsView is of course smooth zooming (like in Google Earth, for example). To achieve this we need to create our own widget inheriting QGraphicsView.

The basic idea is to create new “scaling action” every time user moves his mouse wheel by one “step”. We will store the total number of scalings we have to do in _numScheduledScalings.

Of course, we need to reimplement the wheelEvent. First comes the code, then comes the explanation.

After computing the “intensity” of wheel move and adding it to _numScheduledScalings, we create a QTimeLine object that will invoke scalingTime() function every 20 ms during his 350ms lifespan.

Factor depends on how much we want to zoom the scene. If user wants to zoom it just by a little bit, he will touch mousewheel very delicately. _numScheduledScalings will be small, and so will be factor. On the other hand if user will rotate the wheel intensively, _numScheduledScalings will be bigger, and so we will zoom the scene faster.

Of course, we need to take care of dynamically created QTimeLines:

Categories: