Please nominate your Qt Champions for 2021!

Refer global variables from another javascript file

  • Hi, I have a .js file which build several qml objects and store them in a global array.
    This array is pretty important because all dynamically build objects are accessible with this array.

    Now I'm working in a second .js file and I has to know which objects are existing.
    I would refer the array from the first .js file but I don't get this to work.

    I'm no JS guru or something like that.
    As far as i know the only thing i had to do in HTML is to include both files in a page and thats it.

    But what can I do in the qml environment to access an global array from file 1 in the context of file 2 ?

    Thanks for help

  • Try this?

    I think you just need to add .pragma library reference in your js file and then the vars from it become available for all others.

    Also some usefull info here re .pragma:

  • Ok this works pretty well so far.

    Now I have access but other problems occur.

    Because of the that the javascript libraries are stateless now, I've lost the ability to get informations
    about the qml object itself.
    Ok no problem so far, this could be handled by forwarding the objects as parameters.

    But how to forward imports like enumerations etc. ?

    I used some enums like EventDefinition.Hide from the qml object context.
    But now with the stateless javascript files i can't refer them anymore.

    How to declare or import these enums to the ".pragma library" javascript file.


  • Ok this wasn't so hard ; )

    I just added the imported enum to the parameter list of the functions.

    But I still have a problem with the array of qml objects.

    The first step in the UI build process is to call various functions which build the qml objects and store them to the corresponding global arrays.

    In the next step after all the objects are visible on the screen, I want to call javascript functions on various events of the qml objects.
    Thats not the problem but I have to be able to access the global arrays with all objects. But in the context of the Qml objects these global arrays are empty.

    Although I declare all javascript sources as .pragma library.

    I'm a bit confused right now.

    Has this something to do with the QDeclarativeContext in which the objects were created ?

  • I tried to verify this as far as i could.

    In fact there is some kind of scope problem.
    In my main Qml object lets call it "FirstLayer" I'm call on the Component.onCompleted signal
    a javscript function which builds me different Qml objects using the Qt.createComponent() and component.createObject() functions.

    After this is finisched the "FirstLayer" object has two "Screen" objects and each "Screen" has at least four "Button" objects.

    The build process stores all the child objects like "Screen" and "Button" in the corresponding global javascript arrays. In the context of the "FirstLayer" I can access these arrays perfectly.

    But although I declare the .js file which contains the array as .pragma library I didn't access the same arrays within the context of the "Screen" or "Button" objects. In their context these arrays are still empty.
    So it looks like they are initializing there own instances of these arrays.

    And I have no idea how to solve this.

    So if anybody has an idea how I can fix that.
    Please let me know.

    Thanks for any help guys.

Log in to reply