【初心者向け】Qt Widgetsでアニメーションを駆使する!QGraphicsItemAnimation::item()の使い方をマスターしよう


QGraphicsItemAnimation::item() は、Qt Widgets ライブラリにおける QGraphicsItemAnimation クラスのメソッドで、アニメーション対象となる QGraphicsItem オブジェクトを取得します。このメソッドは、アニメーションを適用するアイテムを指定するために使用されます。

使用方法

QGraphicsItem *item = animation.item();

このコードは、animation オブジェクトがアニメーションを適用している QGraphicsItem オブジェクトを item 変数に格納します。

  • setItem() メソッドを使用して、アニメーション対象となるアイテムを明示的に設定することもできます。
  • item() メソッドは、QGraphicsItemAnimation オブジェクトが作成されたときに自動的に設定されます。
  • QGraphicsItemAnimation クラスは、QGraphicsItem オブジェクトのプロパティを時間をかけて変化させるアニメーションを作成するために使用されます。

次の例では、QGraphicsEllipseItem オブジェクトの位置をアニメーションさせるために QGraphicsItemAnimation クラスを使用します。

#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsEllipseItem>
#include <QGraphicsItemAnimation>

int main(int argc, char *argv[]) {
  QApplication app(argc, argv);

  // シーンを作成
  QGraphicsScene scene;

  // 楕円形アイテムを作成
  QGraphicsEllipseItem *ellipseItem = new QGraphicsEllipseItem(QRectF(-50, -50, 100, 100));
  scene.addItem(ellipseItem);

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

  // アニメーションの設定
  animation.setDuration(2000); // アニメーション時間 (ミリ秒)
  animation.setTargetProperty(QGraphicsItem::X, 100); // X 座標を 100 にアニメーション

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

  // ビューを作成
  QGraphicsView view(&scene);
  view.show();

  return app.exec();
}

この例では、ellipseItem オブジェクトの位置が 2 秒かけて (2000 ミリ秒) X 座標 100 にアニメーションされます。



#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsEllipseItem>
#include <QGraphicsItemAnimation>

int main(int argc, char *argv[]) {
  QApplication app(argc, argv);

  // シーンを作成
  QGraphicsScene scene;

  // 楕円形アイテムを作成
  QGraphicsEllipseItem *ellipseItem = new QGraphicsEllipseItem(QRectF(-50, -50, 100, 100));
  scene.addItem(ellipseItem);

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

  // アニメーションの設定
  animation.setDuration(2000); // アニメーション時間 (ミリ秒)
  animation.setTargetProperty(QGraphicsItem::X, 100); // X 座標を 100 にアニメーション

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

  // ビューを作成
  QGraphicsView view(&scene);
  view.show();

  return app.exec();
}

例2:テキストアイテムの色をアニメーションさせる

この例では、QGraphicsTextItem オブジェクトの色をアニメーションさせるために QGraphicsItemAnimation クラスを使用します。

#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsTextItem>
#include <QGraphicsItemAnimation>

int main(int argc, char *argv[]) {
  QApplication app(argc, argv);

  // シーンを作成
  QGraphicsScene scene;

  // テキストアイテムを作成
  QGraphicsTextItem *textItem = new QGraphicsTextItem("Hello, World!");
  scene.addItem(textItem);

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

  // アニメーションの設定
  animation.setDuration(2000); // アニメーション時間 (ミリ秒)
  animation.setTargetProperty(QGraphicsItem::ItemColor, Qt::red); // 色を赤色にアニメーション

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

  // ビューを作成
  QGraphicsView view(&scene);
  view.show();

  return app.exec();
}

例3:パスアイテムの形状をアニメーションさせる

#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsPathItem>
#include <QGraphicsItemAnimation>
#include <QPainterPath>

int main(int argc, char *argv[]) {
  QApplication app(argc, argv);

  // シーンを作成
  QGraphicsScene scene;

  // パスを作成
  QPainterPath path;
  path.moveTo(0, 50);
  path.lineTo(50, 0);
  path.lineTo(100, 50);

  // パスアイテムを作成
  QGraphicsPathItem *pathItem = new QGraphicsPathItem(path);
  scene.addItem(pathItem);

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

  // アニメーションの設定
  animation.setDuration(2000); // アニメーション時間 (ミリ秒)
  animation.setTargetProperty(QGraphicsItem::Path, path2); // パスを path2 にアニメーション

  // パス2 を作成
  QPainterPath path2;
  path2.moveTo(50, 0);
  path2.lineTo(0, 50);
  path2.lineTo(100, 50);

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

  // ビューを作成
  QGraphicsView view(&scene);
  view.show();

  return app.exec();
}

これらの例は、QGraphicsItemAnimation クラスを使用して様々な種類のアイテムをアニメーションさせる方法を示しています。

  • アイテムを移動しながら回転させる
  • アイテムの透明度をアニメーションさせる
  • アイテムのスケーリングをアニメーションさせる
  • アイテムの回転をアニメーションさせる


以下に、QGraphicsItemAnimation::item() の代替方法をいくつか紹介します。

QGraphicsItemAnimation::targetObject() メソッドを使用する

QGraphicsItemAnimation::targetObject() メソッドは、アニメーション対象となる QObject オブジェクトを取得します。これは、QGraphicsItem オブジェクトだけでなく、他のタイプのオブジェクト (例: QGraphicsItemGroup) もアニメーションさせる場合に役立ちます。

QGraphicsItemAnimation *animation = new QGraphicsItemAnimation();
QGraphicsEllipseItem *ellipseItem = new QGraphicsEllipseItem(QRectF(-50, -50, 100, 100));
scene.addItem(ellipseItem);

animation.setItem(ellipseItem);

// item() メソッドを使用する
QGraphicsItem *item = animation.item();

// targetObject() メソッドを使用する
QObject *targetObject = animation.targetObject();

// item と targetObject は同じオブジェクトを参照する
Q_ASSERT(item == targetObject);

アニメーションの開始前に item() メソッドを使用する

QGraphicsItemAnimation::start() メソッドを呼び出す前に item() メソッドを使用することで、アニメーション対象となるアイテムを取得できます。

QGraphicsItemAnimation *animation = new QGraphicsItemAnimation();
QGraphicsEllipseItem *ellipseItem = new QGraphicsEllipseItem(QRectF(-50, -50, 100, 100));
scene.addItem(ellipseItem);

animation.setItem(ellipseItem);

// アニメーションの開始前に item() メソッドを使用する
QGraphicsItem *item = animation.item();

// アニメーションを開始する
animation.start();

アニメーションシグナルに接続する

QGraphicsItemAnimation クラスは、アニメーションが開始されたときに started() シグナル、アニメーションが終了したときに finished() シグナル、アニメーションの値が更新されたときに valueChanged() シグナルを発行します。これらのシグナルに接続することで、アニメーション対象となるアイテムを取得できます。

QGraphicsItemAnimation *animation = new QGraphicsItemAnimation();
QGraphicsEllipseItem *ellipseItem = new QGraphicsEllipseItem(QRectF(-50, -50, 100, 100));
scene.addItem(ellipseItem);

animation.setItem(ellipseItem);

// started() シグナルに接続する
QObject::connect(animation, &QGraphicsItemAnimation::started, [this]() {
  QGraphicsItem *item = animation.item();
  // ...
});

// finished() シグナルに接続する
QObject::connect(animation, &QGraphicsItemAnimation::finished, [this]() {
  QGraphicsItem *item = animation.item();
  // ...
});

// valueChanged() シグナルに接続する
QObject::connect(animation, &QGraphicsItemAnimation::valueChanged, [this](double value) {
  QGraphicsItem *item = animation.item();
  // ...
});

// アニメーションを開始する
animation.start();

シーンからアイテムを検索する

QGraphicsScene クラスには、シーン内のアイテムを検索するための様々なメソッドが用意されています。これらのメソッドを使用して、アニメーション対象となるアイテムを検索できます。

QGraphicsItemAnimation *animation = new QGraphicsItemAnimation();
QGraphicsEllipseItem *ellipseItem = new QGraphicsEllipseItem(QRectF(-50, -50, 100, 100));
scene.addItem(ellipseItem);

animation.setItem(ellipseItem);

// シーンからアイテムを検索する
QGraphicsItem *item = scene.findItems(QGraphicsItem::ItemIsType, typeid(QGraphicsEllipseItem)).first();

// item と animation.item() は同じオブジェクトを参照する
Q_ASSERT(item == animation.item());

// アニメーションを開始する
animation.start();
  • アニメーション対象となるアイテムを確実に取得するには、item() メソッドを使用するのが最善の方法です。
  • 上記の代替方法は、状況によっては使用できない場合があります。