QScriptEngine engine; QScriptValue printFunction = engine.newFunction([](QScriptContext *context, QScriptEngine *engine) { for (int i = 0; i < context->argumentCount(); ++i) { qDebug() << context->argument(i).toString(); } return engine->undefinedValue(); }); engine.globalObject().setProperty("print", printFunction);
QScriptEngine engine; QScriptValue sqrtFunction = engine.newFunction([](QScriptContext *context, QScriptEngine *engine) { if (context->argumentCount() != 1) { context->throwError("One argument expected for sqrt"); return engine->undefinedValue(); } double value = context->argument(0).toNumber(); return qSqrt(value); }); engine.globalObject().setProperty("sqrt", sqrtFunction);This code creates a new JavaScript function called "sqrt" that takes a single argument and returns the square root of that argument. It also includes error handling in case the wrong number of arguments are passed to the function. The QScriptEngine class is a part of the QtScript module in the Qt library.