このページでは

C

Qul::Singleton Struct

template <typename T> struct Qul::Singleton

このクラスを継承すると、C++ のクラスや構造体をシングルトンとして QML に公開することができます。詳細...

Header: #include <qul/singleton.h>
Since: Qt Quick Ultralite 1.0
Inherits: Qul::Object and Qul::SingletonBase

静的パブリックメンバ

T &instance()

詳細説明

qmlinterfacegenerator ツールはシングルトンから派生したクラスのためにpragma Singletonディレクティブを持つ QML タイプを生成します。

このクラスはCuriously recurring template pattern (CRTP)を使用し、テンプレートパラメータは派生型である必要があります。

最小限の例です:

struct MySingleton : public Qul::Singleton<MySingleton>
{
    Property<int> someProperty;
    int someFunction(int param);
};

C++のコードからシングルトンを使用しないのであれば、上記の例で十分です。そうでない場合は、もう少し完全なものにする必要があります:

struct MySingleton : Qul::Singleton<MySingleton>
{
    // Create friendship for a base class to be able to access private constructor
    friend struct Qul::Singleton<MySingleton>

    Property<int> someProperty;
    int someFunction(int param);

private:
    MySingleton() {}                                // Private constructor
    MySingleton(const MySingleton &);              // Non copy-constructible
    MySingleton &operator=(const MySingleton &);   // Non copyable
};

これでMySingleton 、QMLからもC++からも安全に使えるようになりました:

Item {
    property int otherProperty: MySingleton.someProperty
}

からもC++からも安全に使えるようになりました:

void someFunction() {
    MySingleton::instance().someProperty.setValue(42);
}

Singletonsと QMLでのSingletonsの定義も参照して ください。

メンバ関数ドキュメント

[static] T &Singleton::instance()

シングルトンインスタンスへの参照を返します。

特定の Qt ライセンスの下で利用可能です。
詳細はこちら。