PDA

View Full Version : QScript Date/Array/Object, can't find variable ?!



jh
23rd April 2012, 12:52
hi,

when a qscript is evaluated standard objects cannot be found:

for instance:

function A()
{
// var cdt = new Object();
// var cdt = new Array(2);

var dt = new Date();

var cdt = [2];
cdt[0] = '2012-02-03';

return cdt;

}

what do i have to do that a script can use those types?
best regards,
jh

here's the qscript error:

uncaught exception at line 12
: ReferenceError: Can't find variable: Date

wysota
23rd April 2012, 13:08
You must have messed something up in your code. Date works fine for me.


#include <QScriptEngine>
#include <QtCore>

int main(int argc, char **argv) {
QCoreApplication app(argc, argv);
QScriptEngine engine;
qDebug() << engine.evaluate("new Date()").toString();
return 0;
}

Result: "Mon Apr 23 2012 13:08:22 GMT+0200 (CEST)"

jh
23rd April 2012, 13:58
hi,

thanx for your reply. i found out that 'Date' is not known if i set the 'this' object. otherwise it works.

i do this:

'ObjectWrapper' is a QObject with signals/slots.



ObjectWrapper* qobj = new ObjectWrapper(0,ge);
QScriptValue sobj = _myEngine->newQObject(qobj);
_myEngine->currentContext()->setThisObject( sobj );


regards,
jh

wysota
23rd April 2012, 14:05
Date is a property of the Global Object. By the way, if the above code is the actual code you have then it's probably wrong. I'm assuming the current context of your engine when this code is executed is the global context. In this situation you shouldn't be setting a "this" object. Instead push a new context on the stack and then you'll be able to set "this" object.

jh
23rd April 2012, 14:29
i pushed/poped a new context to the engine and now it works fine.
thanx a lot!
regards,
jh

wysota
23rd April 2012, 14:35
One more thing, for completeness. There are two most commonly used ways of exposing an object to scripting environment. One, more often used, is to expose it as a property of the global object. Then the object is visible throughout the whole life of the engine (or to be more precise of the engine's global object). An alternative is to expose it as a property of a context of the engine. Then the object can be accessed as long as the context is the active context of the engine. What you did is a third way -- you exposed the object as the context itself. Consider using one of the more common methods to avoid naming conflicts.