Using global variables from QML and Javascript
Hi,
In my program i have to access and change the value of a variable from different QML-files and from Javascript-files.
I'm using a JavaScript file to store the value:
Code:
//GlobalData.js
.pragma library
var myValue = 42;
In my QML-files i use the variable like this:
Code:
//example.qml
import "GlobalData.js" as GData
...
Everything works fine. There is only one instance of the GlobalData.js file and every QML-document can read and write myValue.
But now, i included GlobalData.js into a JavaScript like this:
Code:
//myJavaScript.js
Qt.include("GlobalData.js");
console.log(myValue);
The problem is, that myJavaScript.js seems to create its own instance of GlobalData.js, so that if I modify myValue in a QML-Document, it's not changing in myJavaScript.js.
Is there any possibility to let myJavaScript access the same instance of GlobalData.js as the QML-Documents do, instead of creating its own one?
Thank You for your help :)
Re: Using global variables from QML and Javascript
Provide a function in a qml file that will modify that property and call that function from within this JavaScript file. Seems complicated but your design is probably a bit wrong anyway if you have to do something like that.