PDA

View Full Version : [QtScript] One getter/setter function for multiple properties



supergillis
21st May 2009, 10:01
What I am looking for is something like this:

QScriptValue getter(QScriptContext* context, QScriptEngine* engine)
{
QString property = context->getProperty(); // this is what I am looking for
if(property == "id")
return id;
else if(property == "name")
return name;
else if
context->throwError("There is no property like that!");
}
And so you could just do this:

object.setProperty("id", engine.newFunction(getter), QScriptValue::PropertyGetter);
object.setProperty("name", engine.newFunction(getter), QScriptValue::PropertyGetter);
So that you only have to make one getter for two properties.

I hope you understand what I mean!
Gillis

wysota
21st May 2009, 22:33
You should be able to do that using QScriptContext::callee().

supergillis
22nd May 2009, 09:20
You should be able to do that using QScriptContext::callee().
And how should I do that then?

Use callee() to obtain the QScriptValue that represents the function being called. This can for example be used to call the function recursively.
So when I call callee() I get the function being called, but how do I get it's name (which should equal the property)?

wysota
22nd May 2009, 09:52
First of all you can probably store some data of yours there using QScriptValue::setData() while registering the function. If you want to know the function name directly, you'll have to dig into the internals of QScriptValue yourself. Qt sources may be very helpful here.