How can I make a class member function of a C++ plugin scriptable, with complex parameters or return values (such as a lists of vectors or a list of doubles)?
Make C++ plugin function with complex arguments scriptable
Moderator: andrew
Forum rules
Always indicate your operating system and QCAD version.
Attach drawing files, scripts and screenshots.
Post one question per topic.
Always indicate your operating system and QCAD version.
Attach drawing files, scripts and screenshots.
Post one question per topic.
Make C++ plugin function with complex arguments scriptable
From a QCAD developer:
Re: Make C++ plugin function with complex arguments scriptab
Let's have a look at such function from RVector:
In ECMAScript, the function might be called like this:
static QList<double> RVector::getXList(const QList<RVector>& vectors);This function expects a list of vectors as argument and returns a list of doubles.
In ECMAScript, the function might be called like this:
var res = RVector.getXList([new RVector(1,2), new RVector(3,4), new RVector(5,6)]); // res==[1,3,5]The script wrapper looks like this:
QScriptValue REcmaVector::getXList(QScriptContext* context, QScriptEngine* engine) { // check if one argument of type array is given: if (context->argumentCount()==1 && context->argument(0).isArray()) { // convert ECMAScript array to QList<RVector>: QList<RVector> a0; REcmaHelper::fromScriptValue(engine, context->argument(0), a0); // call C++ function: QList<double> cppResult = RVector::getXList(a0); return REcmaHelper::listToScriptValue(engine, cppResult); } else { return REcmaHelper::throwError("Wrong number/types of arguments for RVector.getXList().", context); } }REcmaHelper has many convenience functions to make script wrappers easier to implement. If your custom argument or return type is not supported by REcmaHelper, you might want to model your own helper function, similar like those provided in REcmaHelper.