Difference between revisions of "Smooth Zoom In QGraphicsView"

From Qt Wiki
Jump to: navigation, search
 
(clean-up)
 
(6 intermediate revisions by 4 users not shown)
Line 1: Line 1:
'''English''' [[Smooth Zoom In QGraphicsView Japanese|日本語]]
+
[[Category:HowTo]]
 +
[[Category:snippets]]
 +
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.
  
=Smooth Zoom In 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.
  
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.
+
Of course, we need to reimplement the wheelEvent. First comes the code, then comes the explanation.
  
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.
+
<code>
 +
void MyQGraphicsView::wheelEvent ( QWheelEvent * event )
 +
{
 +
int numDegrees = event->delta() / 8;
 +
int numSteps = numDegrees / 15; // see QWheelEvent documentation
 +
_numScheduledScalings += numSteps;
 +
if (_numScheduledScalings * numSteps < 0) // if user moved the wheel in another direction, we reset previously scheduled scalings
 +
_numScheduledScalings = numSteps;
  
Of course, we need to reimplement the wheelEvent. First comes the code, then comes the explanation.
+
QTimeLine *anim = new QTimeLine(350, this);
 +
anim->setUpdateInterval(20);
  
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.
+
connect(anim, SIGNAL (valueChanged(qreal)), SLOT (scalingTime(qreal)));
 +
connect(anim, SIGNAL (finished()), SLOT (animFinished()));
 +
anim->start();
 +
}
 +
</code>
  
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.
+
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.
  
Of course, we need to take care of dynamically created QTimeLines:<br />
+
<code>
 +
void MyQGraphicsView::scalingTime(qreal x)
 +
{
 +
qreal factor = 1.0+ qreal(_numScheduledScalings) / 300.0;
 +
scale(factor, factor);
 +
}
 +
</code>
  
===Categories:===
+
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.
  
* [[:Category:HowTo|HowTo]]
+
Of course, we need to take care of dynamically created QTimeLines:
* [[:Category:Learning|Learning]]
+
<code>
** [[:Category:Learning::Demos and Examples|Demos_and_Examples]]
+
void MyQGraphicsView::animFinished()
* [[:Category:snippets|snippets]]
+
{
 +
if (_numScheduledScalings > 0)
 +
_numScheduledScalings—;
 +
else
 +
_numScheduledScalings++;
 +
sender()->~QObject();
 +
}
 +
</code>

Latest revision as of 15:02, 24 March 2016

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.

void MyQGraphicsView::wheelEvent ( QWheelEvent * event )
{
 int numDegrees = event->delta() / 8;
 int numSteps = numDegrees / 15; // see QWheelEvent documentation
 _numScheduledScalings += numSteps;
 if (_numScheduledScalings * numSteps < 0) // if user moved the wheel in another direction, we reset previously scheduled scalings
 _numScheduledScalings = numSteps;

 QTimeLine *anim = new QTimeLine(350, this);
 anim->setUpdateInterval(20);

 connect(anim, SIGNAL (valueChanged(qreal)), SLOT (scalingTime(qreal)));
 connect(anim, SIGNAL (finished()), SLOT (animFinished()));
 anim->start();
}

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.

void MyQGraphicsView::scalingTime(qreal x)
{
 qreal factor = 1.0+ qreal(_numScheduledScalings) / 300.0;
 scale(factor, factor);
}

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:

void MyQGraphicsView::animFinished()
{
 if (_numScheduledScalings > 0)
 _numScheduledScalings;
 else
 _numScheduledScalings++;
 sender()->~QObject();
}