Singleton Struct

template <typename T> struct Qul::Singleton

Inherit from this class to expose the C++ class or struct to the QML as a singleton. More...

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

This struct was introduced in Qt Quick Ultralite 1.0.

Static Public Members

T &instance()

Detailed Description

The qmlinterfacegenerator tool will generate QML type with pragma Singleton directive for a class derived from the singleton.

This class use the Curiously recurring template pattern (CRTP) and the template parameter needs to be the derived type.

Minimal example:

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

Above example is just fine if you are not going to use Singleton from C++ code. Otherwise you should make it a bit more complete:

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

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

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

Now MySingleton can be safely used both form QML:

Item {
    property int otherProperty: MySingleton.someProperty

and from C++:

void someFunction() {

See also Singletons.

Member Function Documentation

[static] T &Singleton::instance()

Returns a reference to the singleton instance.

Available under certain Qt licenses.
Find out more.