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

Bo Thorsen bo at fioniasoftware.dk
Thu Oct 13 18:04:40 CEST 2011


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;
}

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.

Bo.

Den 10-10-2011 17:50, ronan.maclaverty at nokia.com skrev:
> Hi all,
>
> I am trying to get access to some global objects from a QML WorkerScript, and whatever I do I cannot seem to access them.  The key object is a C++ object that implements a time consuming function, so I thought this might be the way to go.
>
> The first attempt was to pass the object as inside the workscript message:
>
> MyQml.qml:
>
> Player {
> 	id: player
> }
>
> WorkerScript {
>          id: workerThread
>          source: "qrc:player.js"
> }
>
> workThread.sendMessage( { object: player } );
>
> This did not work - the message.object is not an object.
>
> The second attempt, declare global variables inside the JavaScript file:
>
> player.js:
>
> var myplayer;
>
> MyQml:
>
> import "player.js" as Player
>
> ....
>
> Player.myplayer = player
> workThread.sendMessage( {} );
>
> The WorkScript.onMessage function cannot access player.
>
> I am sure that I have missed some crucial piece of documentation somewhere, but any help solving this issue would help.
>
> Ronan.
> _______________________________________________
> Qt-qml mailing list
> Qt-qml at qt.nokia.com
> http://lists.qt.nokia.com/mailman/listinfo/qt-qml


Bo Thorsen,
Fionia Software.

-- 

Expert Qt and C++ developer for hire
Contact me if you need expert Qt help
http://www.fioniasoftware.dk


More information about the Qt-qml mailing list