C
Qul::Singleton Struct
template <typename T> struct Qul::SingletonInherit 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 and Qul::SingletonBase |
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 : 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
};Now MySingleton can be safely used both form QML:
and from C++:
void someFunction() {
MySingleton::instance().someProperty.setValue(42);
}See also Singletons and Defining Singletons in QML.
Member Function Documentation
[static] T &Singleton::instance()
Returns a reference to the singleton instance.
Available under certain Qt licenses.
Find out more.