// Define a C++ class with a callback function class Calculator { public: Calculator() {} int add(int a, int b, const QScriptValue& callback) { int result = a + b; if (callback.isFunction()) { QObject* cbObject = QScriptValueToQObject(callback); QMetaObject::invokeMethod(cbObject, "operator()", Qt::QueuedConnection, Q_ARG(int, result)); } return result; } }; // Prepare a JavaScript function to use as a callback QScriptEngine engine; QScriptValue callback = engine.evaluate("function(result) { console.log('Result: ' + result); }"); // Make a call to the add function, passing in the callback function QScriptValue result = engine.evaluate("calculator.add(2, 3, callback);"); // Output the result qDebug() << "Result: " << result.toNumber();In this example, the Calculator class has an add function that takes two integers and a callback function as arguments. The function adds the integers and if a callback function is provided, it converts it to a QObject using QScriptValue toQObject, and then calls the function using the Qt signal/slot mechanism. The JavaScript code creates a new instance of the Calculator class called "calculator", and defines a callback function using engine.evaluate. It then calls the add function on the calculator object, passing in the callback function as an argument. Finally, it logs the result to the console and outputs it to the debug log. The QScriptValue toQObject function is used inside the add function to convert the callback function to a QObject. This allows the C++ code to call the function on the JavaScript side, passing in the result as a parameter. The QScriptValue toQObject function is part of the QtScript module in C++.