【初心者向け】Qt WidgetsにおけるQGraphicsRotation::axisChanged() - 回転軸変更のシグナルを理解して使いこなす


QGraphicsRotation::axisChanged() は、Qt Widgets ライブラリにおける QGraphicsRotation クラスのシグナルです。このシグナルは、QGraphicsRotation オブジェクトの 回転軸 が変更されたときにemitされます。

回転軸とは?

回転軸は、オブジェクトが回転する軸を定義します。デフォルトでは、QGraphicsRotation オブジェクトの回転軸は Z 軸 です。

シグナルの接続

axisChanged() シグナルは、QObject::connect() 関数を使用してスロットに接続できます。スロットは、シグナルがemitされたときに呼び出される関数です。

QGraphicsRotation rotation;

QObject::connect(&rotation, &QGraphicsRotation::axisChanged, this, &MyClass::onAxisChanged);

void MyClass::onAxisChanged()
{
    // 回転軸が変更されたときに実行する処理を記述
}

この例では、MyClass クラスの onAxisChanged() メソッドが axisChanged() シグナルに接続されています。onAxisChanged() メソッドは、シグナルがemitされたときに呼び出され、回転軸が変更されたことを処理します。

シグナルのemit

axisChanged() シグナルは、QGraphicsRotation::setAxis() メソッドを使用して明示的にemitできます。

QGraphicsRotation rotation;

rotation.setAxis(Qt::XAxis);

// シグナルがemitされる

この例では、QGraphicsRotation オブジェクトの回転軸が X 軸 に設定されます。この操作により、axisChanged() シグナルがemitされます。

QGraphicsRotation::axisChanged() シグナルは、QGraphicsRotation オブジェクトの回転軸が変更されたときに通知するために使用されます。このシグナルを使用して、回転軸の変化に反応するアプリケーションロジックを実装できます。



例 1: 回転軸を X 軸に変更し、シグナルのスロットを呼び出す

#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsItem>
#include <QGraphicsRotation>

class MyItem : public QGraphicsItem
{
public:
    MyItem() {
        setFlag(QGraphicsItem::ItemIsMovable);
    }

    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override {
        painter->setBrush(Qt::red);
        painter->drawRect(-50, -50, 100, 100);
    }
};

class MyClass : public QObject
{
public:
    MyClass() {
        scene = new QGraphicsScene;
        view = new QGraphicsView(scene);

        item = new MyItem;
        scene->addItem(item);

        rotation = new QGraphicsRotation;
        rotation->setObject(item);

        QObject::connect(rotation, &QGraphicsRotation::axisChanged, this, &MyClass::onAxisChanged);

        view->show();
    }

public slots:
    void onAxisChanged() {
        qInfo() << "回転軸が変更されました: " << rotation->axis();
    }

private:
    QGraphicsScene *scene;
    QGraphicsView *view;
    MyItem *item;
    QGraphicsRotation *rotation;
};

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

    MyClass myClass;

    return app.exec();
}

この例では、MyItem クラスという名前のグラフィックスアイテムを作成し、シーンに追加します。次に、QGraphicsRotation オブジェクトを作成し、MyItem オブジェクトに設定します。axisChanged() シグナルは、MyClass クラスの onAxisChanged() メソッドに接続されます。

onAxisChanged() メソッドは、シグナルがemitされたときに呼び出され、回転軸が変更されたことをログに記録します。

この例を実行すると、赤い四角形のグラフィックスアイテムが表示されます。アイテムをドラッグして回転させると、axisChanged() シグナルがemitされ、ログに回転軸が変更されたことが記録されます。

#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsItem>
#include <QGraphicsRotation>
#include <QSlider>

class MyItem : public QGraphicsItem
{
public:
    MyItem() {
        setFlag(QGraphicsItem::ItemIsMovable);
    }

    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override {
        painter->setBrush(Qt::red);
        painter->drawRect(-50, -50, 100, 100);
    }
};

class MyClass : public QObject
{
public:
    MyClass() {
        scene = new QGraphicsScene;
        view = new QGraphicsView(scene);

        item = new MyItem;
        scene->addItem(item);

        rotation = new QGraphicsRotation;
        rotation->setObject(item);

        slider = new QSlider(Qt::Horizontal);
        slider->setRange(0, 2);
        slider->setValue(1); // デフォルトの回転軸は Z 軸

        QObject::connect(slider, &QSlider::valueChanged, this, &MyClass::onValueChanged);

        view->show();
    }

public slots:
    void onValueChanged(int value) {
        switch (value) {
            case 0:
                rotation->setAxis(Qt::XAxis);
                break;
            case 1:
                rotation->setAxis(Qt::YAxis);
                break;
            case 2:
                rotation->setAxis(Qt::ZAxis);
                break;
        }
    }

private:
    QGraphicsScene *scene;
    QGraphicsView *view;
    MyItem *item;
    QGraphicsRotation *rotation;
    QSlider *slider;
};

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


QGraphicsRotation::setAxis() メソッドの監視

QGraphicsRotation::axisChanged() シグナルを使用する代わりに、QGraphicsRotation::setAxis() メソッドを直接監視することができます。このメソッドは、オブジェクトの回転軸を設定するために使用されます。

QGraphicsRotation rotation;

rotation.setObject(item);

QObject::connect(&rotation, &QGraphicsRotation::setAxis, this, &MyClass::onAxisChanged);

void MyClass::onAxisChanged(Qt::Axis axis) {
    // 回転軸が変更されたことを処理
}

この例では、setAxis() メソッドが呼び出されるたびに onAxisChanged() メソッドが呼び出されます。このメソッドは、axis パラメータを使用して新しい回転軸を取得できます。

QProperty::valueChanged() シグナルの使用

QGraphicsRotation クラスは、axis プロパティを公開しています。このプロパティは、オブジェクトの回転軸を表します。QProperty::valueChanged() シグナルを使用して、このプロパティの変更を監視することができます。

QGraphicsRotation rotation;

rotation.setObject(item);

QProperty property(&rotation, "axis");

QObject::connect(&property, &QProperty::valueChanged, this, &MyClass::onAxisChanged);

void MyClass::onAxisChanged(const QVariant &value) {
    Qt::Axis axis = value.value<Qt::Axis>();
    // 回転軸が変更されたことを処理
}

この例では、axis プロパティの変更が検出されると onAxisChanged() メソッドが呼び出されます。このメソッドは、value パラメータを使用して新しい回転軸を取得できます。

カスタムシグナルの作成

独自のシグナルを作成して、回転軸が変更されたことを通知することもできます。この方法は、より柔軟な制御が必要な場合に役立ちます。

class MyGraphicsRotation : public QGraphicsRotation
{
public:
    using QGraphicsRotation::setAxis;

    QSignalEmitter<Qt::Axis> axisChangedSignal;

protected:
    void setAxis(Qt::Axis axis) override {
        QGraphicsRotation::setAxis(axis);
        emit axisChangedSignal(axis);
    }
};

class MyClass : public QObject
{
public:
    MyClass() {
        scene = new QGraphicsScene;
        view = new QGraphicsView(scene);

        item = new MyItem;
        scene->addItem(item);

        rotation = new MyGraphicsRotation;
        rotation->setObject(item);

        QObject::connect(rotation, &MyGraphicsRotation::axisChangedSignal, this, &MyClass::onAxisChanged);

        view->show();
    }

public slots:
    void onAxisChanged(Qt::Axis axis) {
        // 回転軸が変更されたことを処理
    }

private:
    QGraphicsScene *scene;
    QGraphicsView *view;
    MyItem *item;
    MyGraphicsRotation *rotation;
};

この例では、MyGraphicsRotation という名前のカスタムクラスを作成しています。このクラスは、setAxis() メソッドをオーバーライドし、axisChangedSignal シグナルをemitします。このシグナルは、axis パラメータを使用して新しい回転軸を伝えます。