Unsolved Javascript shared variables between QML
-
Hello,
I have a first js file that get a list and fill a libraryModel after a Json request.
When the user click on a row in the TableView the js file will send it to another js file that it set as a library (as I saw on internet, this ".pragma library" allow me to shared variable)
But when I try to access to a variable in the library js file, I didnt receive what i wanted.
My library js file :
.pragma library var index; function setIndex(r){ index = r; console.log("set row index : "+index) } function getIndex(){ console.log("return index js : "+index) return index }
I got this on the console :
Click on my first QML file = qml: set row index : 1
Access to my next QML file = qml: return index js : undefinedThank for your help
-
Hi,
How are you using js inside your second QML file? I think in your case you need something like this:
import "path_to_your_js_file" as SharedLib //...... Component.onCompleted: { console.log(SharedLib.index); } //.....
By the way you don't need setter and getter methods because your variable is public.
-
@shav That's exactly what I have but it doesnt work
-
Why you use JS for sharing variable between qml files? You can use id of cml items inside your qml files. For example if you have qml file like this:
import QtQuick 2.0 Item { id: item1 property var prop1: ({key1: "value1"}) SomeAnotherQml { } }
SomeAnotherQml.qml implementation:
import QtQuick 2.0 Item { id: secondQmlItem property var prop2: null Component.onCompleted: { console.log("item1: "+JSON.stringyfire(item1.prop1)); console.log("prop2: "+JSON.stringyfire(prop2)); } }
In console you must see the value of prop1 from item1 also you can add property to your second qml file and send it like this:
SomeAnotherQml { prop2: item1.prop1 }
-
About your solution, I tried it before, many times, but I dont really know how to use it in my case and because I have to finish the project quickly, I dont have time to understand how use it.
Thank for your help
My problem is to set variable from js file to library js file, I dont know why it doesnt work