[Qt-qml] Getting access to QML objects from a workerscript

Sivan Greenberg sivan at omniqueue.com
Thu Oct 13 18:18:05 CEST 2011


On Thu, Oct 13, 2011 at 6:04 PM, Bo Thorsen <bo at fioniasoftware.dk> wrote:
> I have used the idea to store objects in javascript for global access
> before. You have to make sure you have ".pragma library" in the JS file,
> though, or you will have each JS import declare it's own name scope. Not
> what you want for a global variable :)
>
> The JS file looks something like this:
>
>
> .pragma library
>
> var globalObject;
>
> function getGlobalObject() {
>   return globalObject;
> }
>
> function setGlobalObject(object) {
>   return globalObject;
> }
>

If you declare your object to be well, through the source location,
global, and using .pragma then why do you need the functions to access
it? Also, does you approach allows passing the whole object including
methods (so you can manipulate them for instance) as opposed to just
passing values and responses as strings as noted in WorkerScript  qt
docs?

> In each QML file, you import this one and then you have access to your
> object.
>
> I think this is a horrible piece of code, but it's the only way I have
> found so far.
>

> If possible, move the shared values you have to a C++ object instead and
> declare this as a named global property. This is a cleaner way to access
> shared values. But it only works on limited data types.

I was sure that due to the nature of objects serialization in JS, this
would actually be the only way to pass all of the types to the worker
script?


Thanks for the note!

-Sivan


More information about the Qt-qml mailing list