[Development] QML Singletons

Knoll Lars Lars.Knoll at digia.com
Tue Dec 11 10:23:22 CET 2012


On Dec 11, 2012, at 10:18 AM, Thomas Hartmann <Thomas.Hartmann at digia.com>
 wrote:

> Hi,
> 
>> 
>> it's a bit weird that you need a qmldir for that. Wouldn't it be better if we could mark this as singleton in the implementation (ie. inside single.qml)? Maybe use a new keyword for that? static is already a reserved keyword in Ecmascript 5.1, so we could maybe write single.qml as:
>> 
>> static QtObject {
>> 	property int myproperty;
>> 	...
>> }
> 
> 
> But how would the engine know about single.qml being static? AFAIK all 
> .qml parsing is done on demand. The engine would have to know that 
> single.qml is a singleton from another source (Or scan all .qml files 
> upfront for the static keyword).

Sure, but it won't matter until you reference Single somewhere. The singleton should not get loaded before that anyway. And once you have a reference to it, the QML engine will try to load the file and then figure out it's static. That would require some work in the engine, but I think it might be doable.

Cheers,
Lars

> 
> Another approach could be having something like a Singleton.qml in the 
> plugin.
> 
> Singleton.qml would be the general entry point to instantiate QML 
> singletons for a plugin.
> 
> Singleton.qml
> 
> Singletons {
>     MySingleton01 {
>         id: mySingleton01
>     }
> 
>     MySingleton02 {
>         id: mySingleton02
>     }
>     QtObject {
>         id: mySingleton03
>     }
>     ...
> }
> 
> Everything defined in Singletons would be instantiated once in the root 
> context.
> 
> Kind Regards,
> Thomas Hartmann
> _______________________________________________
> Development mailing list
> Development at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/development




More information about the Development mailing list