The replacement of "window" object in Javascript


  • Qt Champions 2016

    Whatever we declared a global variable in Javascript under browser, it will be added to the "window" object automatically. Then we could check did a global variable/object declared by looking at the window object.

    However, it don't have "window" object in Qt Quick. Do it have any replacement? So that I can check did a global variable declared?

    Thanks.


  • Moderators

    In Qt Quick, global stuff (for QML and JS) is added to the rootContext of the QML engine. You access it directly, without specifying any parent object.

    As for pure JavaScript, AFAIK, all contexts are separate, so you can't specify anything truly global. But my knowledge of JS is too limited to say for sure.


  • Qt Champions 2016

    Thanks for your reply. In fact, I would like to configure the variable of a Javascript instance and the method I chosen is context object. However, as the context object is passed by the C++ program, it will throw "Reference Error" under Qt Designer.

    To avoid the error , I would like to check did the context object / global variable declared before access. But unfortunately it don't have the "window" object under Qt Quick.


  • Moderators

    @
    if (someObject == undefined) {
    // it is not defined
    } else {
    // we are good to go :)
    }
    @


  • Qt Champions 2016

    That will throw :

    @
    ReferenceError: someObject is not defined
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.