Qt Widgetsでアイテムをせん断アニメーションさせる:QGraphicsItemAnimation::shearList()の解説とサンプルコード


QGraphicsItemAnimation::shearList() は、QGraphicsItemAnimation クラスのメソッドで、アニメーションの特定のステップにおけるアイテムのせん断量を取得するために使用されます。せん断は、アイテムを傾ける視覚効果を作成するために使用されます。

使用方法

このメソッドを使用するには、まず QGraphicsItemAnimation オブジェクトを作成し、アニメーションさせたいアイテムを設定する必要があります。次に、shearList() メソッドを呼び出すことで、せん断量のリストを取得できます。リストは、QPair<qreal, QPointF> オブジェクトのリストで構成されます。最初の qreal 値はステップ値を表し、2番目の QPointF 値はせん断量を表します。

QGraphicsItemAnimation* animation = new QGraphicsItemAnimation;
animation->setItem(item);

// アニメーションのステップ 0.5 でのせん断量を取得
QPair<qreal, QPointF> shear = animation->shearList()[0];

// 水平方向に 2 ユニット、垂直方向に 1 ユニットせん断
shear.second = QPointF(2.0, 1.0);

// せん断量をアニメーションに設定
animation->setShearAt(0.5, shear.second);
  • せん断量を設定するには、setShearAt() メソッドを使用します。
  • shearList() メソッドは、明示的に挿入されたせん断量のみを返します。タイムラインによって自動的に生成されたせん断量は返されません。


#include <QGraphicsItemAnimation>
#include <QGraphicsItem>
#include <QPointF>

int main() {
  // アイテムを作成
  QGraphicsItem* item = new QGraphicsItem();

  // アニメーションを作成
  QGraphicsItemAnimation* animation = new QGraphicsItemAnimation;
  animation->setItem(item);

  // アニメーションの持続時間を設定
  animation->setDuration(1000);

  // アニメーションのステップごとにせん断量を設定
  for (int i = 0; i <= 10; ++i) {
    qreal step = i / 10.0;
    qreal shearX = step * 2.0;  // 水平方向せん断量
    animation->setShearAt(step, QPointF(shearX, 0.0));
  }

  // アニメーションを開始
  animation->start();

  return 0;
}

例2:アイテムを回転とせん断でアニメーションする

この例では、QGraphicsItemAnimation を使用して、アイテムを回転とせん断でアニメーションします。

#include <QGraphicsItemAnimation>
#include <QGraphicsItem>
#include <QPointF>

int main() {
  // アイテムを作成
  QGraphicsItem* item = new QGraphicsItem();

  // アニメーションを作成
  QGraphicsItemAnimation* animation = new QGraphicsItemAnimation;
  animation->setItem(item);

  // アニメーションの持続時間を設定
  animation->setDuration(2000);

  // アニメーションのステップごとに回転とせん断量を設定
  for (int i = 0; i <= 20; ++i) {
    qreal step = i / 20.0;
    qreal angle = 360.0 * step;  // 回転角度
    qreal shearX = step * 2.0;  // 水平方向せん断量
    qreal shearY = step;  // 垂直方向せん断量
    animation->setRotationAt(step, angle);
    animation->setShearAt(step, QPointF(shearX, shearY));
  }

  // アニメーションを開始
  animation->start();

  return 0;
}


QPropertyAnimation を使用する

QPropertyAnimation は、プロパティ値をアニメーションさせるためのクラスです。QGraphicsItem のプロパティをアニメーションさせるために使用できます。例えば、以下のコードは、アイテムの位置をアニメーションさせます。

QPropertyAnimation* animation = new QPropertyAnimation(item, "pos");
animation->setDuration(1000);
animation->setStartValue(QPointF(0, 0));
animation->setEndValue(QPointF(100, 100));
animation->start();

QPropertyAnimation は、QGraphicsItemAnimation よりも柔軟性が高く、より複雑なアニメーションを作成することができます。

QStateMachine を使用する

QStateMachine は、状態遷移ベースのアニメーションを作成するためのクラスです。QGraphicsItem の状態をアニメーションさせるために使用できます。例えば、以下のコードは、アイテムの色を赤から緑に変化させるアニメーションを作成します。

QStateMachine* machine = new QStateMachine(item);

QState* redState = new QState(machine);
redState->assignProperty(item, "color", Qt::red);

QState* greenState = new QState(machine);
greenState->assignProperty(item, "color", Qt::green);

redState->addTransition(greenState);
greenState->addTransition(redState);

machine->setStartState(redState);
machine->start();

QStateMachine は、QGraphicsItemAnimationQPropertyAnimation よりも複雑ですが、非常に複雑なアニメーションを作成することができます。

カスタムアニメーションを実装する

上記の方法で実現できないアニメーションを作成する場合は、カスタムアニメーションを実装する必要があります。これには、タイマーと QGraphicsItem のプロパティを直接操作するコードが必要です。