Is it possible, in Qt 4.8, from the C++ side, to set QScriptValue
-local values in a QScriptEngine
?
For example, let's say I have:
QScriptEngine engine;
engine.globalObject().setProperty("glob", 1000);
// ???: Set loc to, say, 42.
QScriptValue expr1 = engine.evaluate("glob + loc");
qDebug() << expr1.toNumber();
// ???: Set loc to, say, 99.
QScriptValue expr2 = engine.evaluate("glob + loc");
qDebug() << expr2.toNumber();
And I'd like the output of that to be:
1042
1099
The obvious solution to the contrived example above is to just make "loc" global and set + reset it accordingly, but for reasons that distract from this question that's not what I'm looking for. I'd like:
- "loc" to be local to each
QScriptValue
- To not artificially modify the script strings by e.g. prepending
QString().sprintf("loc = %d;", 42)
or whatever to the scripts. - To only use a single
QScriptEngine
.
The only thing I could really think of to try was:
QScriptValue expr1 = engine.evaluate("glob + loc");
expr1.setProperty("loc", 42);
qDebug() << expr1.toNumber();
But it seems the value is already fully evaluated by evaluate()
and so setProperty()
has no effect (no surprise there).
I also had a peek at QScriptProgram
hoping it provided a program-local environment but alas.
I am looking at the docs, and will continue to look at them after I post this, but this is the first time I'm using QScriptEngine
and I have to admit my brain is turning to mush, so I apologize if it's right there and totally obvious. I will accept RTFM as a valid answer.