Nominate our 2022 Qt Champions!

How to set dynamic Property for QJSEngine

  • I would like to add a dynamic properties to the java script function that will be evaluated by a QJSEngine.

    QString formula = "_X_*_X_";    //initial value of _X_ is 5
    QString jsCode = QString("(function myFunction() {return (%1);})();").arg(formula);
    QJSValue fun = m_jsengine->evaluate(jsCode);
    m_jsengine->globalObject().setProperty("_X_",QJSValue(QString("%1").arg(10));;                //variable X is not changed to 10

    why the set property for the engine does not work here. Does will not evaluate the jscode again with the update property? If not then how to tell the engine to take the new value for X instead of the old value.

  • Any hints on the above question will be helpful for me to proceed.

  • Moderators

    see QTBUG-38181
    It's a known limitation of QJSEngine.
    If you can, switch to QScriptEngine (QML).