Sun睿
Sun睿

Reputation: 1

How does QScriptEngine synchronize dynamic properties to C++

I have a scenario where I need to add properties dynamically in a script QScriptEngine After dynamically adding properties, the script assigns a value to this variable. How do I synchronize this value to C++

I started with QJSEngine, but it doesn't seem to support dynamically adding properties, so I switched to QScriptEngine to dynamically add property values, but changes to variables in the script are not synchronized with c++

#include <QtScript\QScriptEngine>
#include <QDebug>
int main(int argc, char *argv[])
{
    QScriptEngine engine;
    int num = 0;
    engine.globalObject().setProperty("num", num);
    auto res = engine.evaluate("num = 3");
    if (res.isError())
    {
        qDebug() << res.property("message").toString();
    }

}

In a flash, I call engine.globalObject().property("num").toVariant().toInt() after evaluate; I can get the value of this property in the script, because my "num" variable is probably added to an array at run time, and it may not be convenient to use property("num").tovariant ().toint (). I wonder if there is any other way to automatically synchronize the variable besides this method

Upvotes: 0

Views: 18

Answers (0)

Related Questions