PDA

View Full Version : QJSEngine call method with variable arguments number



GuillaumeG
1st April 2015, 16:55
Hi,

I use QJSEngine and I want to add to the engine a method with variable arguments number.

I have added methods of C++ class "CustomScripts" with this code and it works fine, I can call myFunction.


CustomScripts * scriptObject = new CustomScripts;
QJSValue scriptTest = myQJSEngine->newQObject(scriptObject);
QJSValue aFunction = scriptTest.property("myFunction");
myQJSEngine->globalObject().setProperty("myFunction", aFunction);

But exposed methods take only 1 or 2 arguments.

I try to use QVariantList to pass variable argument number but it only work for javascript array.
How can I do that ?

wysota
1st April 2015, 19:24
Is the method written in C++ or JavaScript? How are you trying to call it?

anda_skoa
2nd April 2015, 07:56
In QtScript you could do that through QScriptContext.

QJSEngine doesn't any of the integration API (yet), so you probably have to work around these limitations.
Maybe create a function in JavaScript that converts alls its arguments into an array and then calls the C++ function.

Cheers,
_

GuillaumeG
2nd April 2015, 08:44
Thanks, your right.

If I use a javascript method to wrap the C++ callback, it works.
Here my code, with "printCallback" a C++ method exposed in javascript.


QJSValue print = fEngine->evaluate("function() { printCallback(Array.prototype.slice.apply(argument s));}");
fEngine->globalObject().setProperty("print", print);