Important: Please read the Qt Code of Conduct -

QJSEngine set property before Function call

  • I have the following code to evaluate a code in QJSEngine:-

    QString jsCode = QString("(function myFunction() {return (%1);})();").arg(code);
    m_jsengine->globalObject().setProperty("myFunction", jsCode);
    QJSValue fun = m_jsengine->evaluate(jsCode);
    for (int i = 0; i < 500; ++i)  {
          m_jsengine->globalObject().setProperty("_X_", QString("%1").arg(i));
          if( i == 10 )  {
             qDebug() << "_X_ Value is : " << m_jsengine->globalObject().property("_X_").toString();

    where code is the formula. (ex.A*B/2) with initial value A as 10 and B as 12. When I execute the code, the expected result is, each time the X changes and the output should change. But the result is always the same. So I knew the set property in the loop really did not change the value of X variable.

    A little help is highly appreciated.

Log in to reply