このページでは

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++ コードから Singleton を使用しない場合は、上記の例で問題ありません。そうでない場合は、もう少し完全な形にする必要があります:

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側からも

Item {
    property int otherProperty: MySingleton.someProperty
}

C++側からも安全にxml-ph-0000@deepl.internalを使用できるようになります:

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

シングルトン」および「QMLでのシングルトンの定義も参照してください

メンバ関数のドキュメント

[static] T &Singleton::instance()

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

特定のQtライセンスの下で利用可能です。
詳細はこちらをご覧ください。