Important: Please read the Qt Code of Conduct -

Javascript-References to QML objects get lost

  • Hi,

    I build an QML/Javascript application. The javascript-file is included using:
    @import "vp.js" as VpScript@

    In vp.js I can access all qml objects using their ID. I suppose this is the right way!?
    After starting the application this works great, but after about 1 minute and 40 seconds, javascript seems to loose all references to qml objects. If I try to acces an object using the ID (e.g. console.log(header);) it returns undefined.
    There are no javascript or application errors.

    All javascript-code embedded in the qml-file works well at this time.
    E.g. this will work:
    @Timer {
    interval: 1000; running: true; repeat: true
    onTriggered: {

    But calling a function in vp.js, which contains console.log(), will output "undefined":
    @Timer {
    interval: 1000; running: true; repeat: true
    onTriggered: VpScript.test(header);

    Any suggestions?

  • This is really strange, that something changes after 1:40, but I suggest you don't use objects by ID unless they are in the same QML file. You can always pass them as parameter to js. Or you just can embed your js code into QML file. What's the point of separate js files?

  • Thanks for your reply.
    I wanted to separate QML and JS code, so I used two files. Now I moved all JS-code to the QML-file and everything works fine.

    Still strange, but ok...

Log in to reply