📢 Webサイト閉鎖と移転のお知らせ
このWebサイトは2026年9月に閉鎖いたします。
新しい記事は移転先で追加しております。(旧サイトでは記事を追加しておりません)

150行目: 150行目:
<u>上記で示したように、<code>create()</code>メソッドのパラメータでエンジンのIDやスレッドアフィニティを確認することにより、アサーションすることができる。</u><br>
<u>上記で示したように、<code>create()</code>メソッドのパラメータでエンジンのIDやスレッドアフィニティを確認することにより、アサーションすることができる。</u><br>
<br><br>
<br><br>
== 例: 複数のQMLから同一のシングルトンクラスを参照する ==
以下の例では、2つの画面 (Screen1.qmlとScreen2.qml) から同一のシングルトンクラスを参照する。<br>
<br>
C++でシングルトンクラスを定義および生成して、QMLエンジンに登録する必要がある。<br>
<br>
まず、C++でシングルトンクラスを定義する。<br>
<syntaxhighlight lang="c++">
// hoge.cpp
#include "hoge.h"
Hoge::Hoge(QObject *parent) : QObject(parent), m_message("Hello from Hoge!")
{
}
Hoge& Hoge::getInstance()
{
    static Hoge instance;
    return instance;
}
QString Hoge::message() const
{
    return m_message;
}
void Hoge::setMessage(const QString &message)
{
    if (m_message != message) {
      m_message = message;
      emit messageChanged();
    }
}
</syntaxhighlight>
<br>
<syntaxhighlight lang="c++">
// hoge.h
#ifndef HOGE_H
#define HOGE_H
#include <QObject>
class Hoge : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QString message READ message WRITE setMessage NOTIFY messageChanged)
private:
    QString m_message;
private:
    Hoge(QObject *parent = nullptr);
    ~Hoge() = default;
    Hoge(const Hoge&) = delete;
    Hoge& operator=(const Hoge&) = delete;
public:
    static Hoge& getInstance();
    QString message() const;
    void setMessage(const QString &message);
signals:
    void messageChanged();
};
#endif
</syntaxhighlight>
<br>
次に、main関数において、QMLエンジンにシングルトンクラスを登録する。<br>
<syntaxhighlight lang="c++">
// main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include "hoge.h"
int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);
    QQmlApplicationEngine engine;
    // シングルトンクラスをQMLに登録
    engine.rootContext()->setContextProperty("hoge", &Hoge::getInstance());
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    return app.exec();
}
</syntaxhighlight>
<br>
複数のQMLファイルからシングルトンクラスを使用する。<br>
<syntaxhighlight lang="qml">
// Screen1.qml
import QtQuick
import QtQuick.Controls
Item {
    width: 300
    height: 200
    Text {
        anchors.centerIn: parent
        text: hoge.message
    }
    Button {
        anchors.top: parent.top
        anchors.horizontalCenter: parent.horizontalCenter
        text: "Set Message from Screen1"
        onClicked: hoge.setMessage("Message set from Screen1")
    }
}
</syntaxhighlight>
<br>
<syntaxhighlight lang="qml">
// Screen2.qml
import QtQuick
import QtQuick.Controls
Item {
    width: 300
    height: 200
    Text {
        anchors.centerIn: parent
        text: hoge.message
    }
    Button {
        anchors.bottom: parent.bottom
        anchors.horizontalCenter: parent.horizontalCenter
        text: "Set Message from Screen2"
        onClicked: hoge.setMessage("Message set from Screen2")
    }
}
</syntaxhighlight>
<br>
最後に、メイン画面 (main.qml) において、上記の2つの画面を表示する。<br>
<syntaxhighlight lang="qml">
// main.qml
import QtQuick
import QtQuick.Window
Window {
    width: 600
    height: 400
    visible: true
    title: qsTr("Hoge Singleton Example")
    Row {
      anchors.fill: parent
      Screen1 {
          width: parent.width / 2
          height: parent.height
      }
      Screen2 {
          width: parent.width / 2
          height: parent.height
      }
    }
}
</syntaxhighlight>
<br><br>


__FORCETOC__
__FORCETOC__
[[カテゴリ:Qt]]
[[カテゴリ:Qt]]