Smooth Zoom In QGraphicsView/ja

From Qt Wiki
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
This article may require cleanup to meet the Qt Wiki's quality standards. Reason: Auto-imported from ExpressionEngine.
Please improve this article if you can. Remove the {{cleanup}} tag and add this page to Updated pages list after it's clean.

English 日本語

QGraphicsView でスムースなズーム機能の作り方

QGraphicsView を使ったプログラムですばらしい機能とはもちろん(例えば、Google Earth のような)スムースなズーム機能です。その実装のためには、QGraphicsView を継承した独自ウィジェットを作成する必要があります。

その基本的な考えはユーザがマウスのホイールをひとつ「動かす」たびに、新たに「スケーリングアクション」を作成するというものです。スケーリングする合計数は _numScheduledScalings に格納します。

もちろん、wheelEvent の再実装が必要です。まずはそのコードを示します。説明はその後に行います。

void MyQGraphicsView::wheelEvent ( QWheelEvent * event )
{
 int numDegrees = event->delta() / 8;
 int numSteps = numDegrees / 15; // QWheelEvent のドキュメントを参照してください
 _numScheduledScalings ''= numSteps;
 if (_numScheduledScalings * numSteps < 0) // ホイールが逆方向に動いた場合には、それまでに予定していたスケーリングをリセットします
 _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();
}

ホイールの移動の「勢い」を計算して、_numScheduledScalings に加えます。Doc:QTimeLine のオブジェクトを作成して、 350ms の寿命の間に 20ms 毎に scalingTime() 関数を呼び出します。

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

倍率(factor)はシーンをどのくらいズームしたいかによって決まります。ほんの少しだけズームしたいときには、ホイールをとても繊細に動かさなくてはならないでしょう。_numScheduledScalings は小さくなり、 factor はほとんど 1 になるでしょう。その逆に、ホイールを勢いよく回転させた場合には、_numScheduledScalings は大きくなり、シーンのズームは速くなるでしょう。

もちろん、動的に作成した QTimeLine の処理も必要です。 void MyQGraphicsView::animFinished() {

if (_numScheduledScalings > 0)
_numScheduledScalings—;
else
_numScheduledScalings++;
sender()->~QObject();

}