All day when I try to get this code to work. It should be the same code provided on the QScript help page but unfortunately it doesn't work at all!
class Person
{
public:
QString nm;
Person()
{
}
Person(QString& name)
:nm(name)
{
}
};
Q_DECLARE_METATYPE(Person)
Q_DECLARE_METATYPE(Person*)
QScriptValue Person_ctor(QScriptContext* c,QScriptEngine* e)
{
QString x = c->argument(0).toString();
return e->toScriptValue(Person(x));
}
QScriptValue Person_prototype_toString(QScriptContext* c,QScriptEngine* e)
{
Person* per = qscriptvalue_cast(c->thisObject());
qDebug(qPrintable(per->nm));
return e->undefinedValue();
}
....
QScriptValue per_ctr = eng->newFunction(Person_ctor);
per_ctr.property("prototype").setProperty("toString",eng->newFunction(Person_prototype_toString));
per_ctr.property("prototype").setProperty("myPrint",eng->newFunction(Person_prototype_toString));
eng->globalObject().setProperty("Person",per_ctr);
...
If I try to evaluate the following code in JavaScript
var p = new Person("Guido");
p.toString();
p.myPrint();
I should get:
Guido
Guido
instead of what I really get, this is a white line from the toString function (probabily calls the Object.toString function) and "Interpreter error: line 2: TypeError: The result of the expression" p.myPrint [not defined] and not the function ". error message from myPrint. I suppose I didn’t connect the two functions correctly to the Person prototype, even if I tried to follow the pages of the documentation ... PLEASE Can someone explain to me what my fault is?!? Thanks!