Important: Please read the Qt Code of Conduct -

C++ callback in JS using QScriptValue

  • Hi,
    I can call JS function with arguments and get the result using QScriptValue but question is: how to send an argument as a C++ callback?

    My JS script:

    function test(testCallback)

    And my callback (C++):

    void Test::testCallback(const qint32& value)
    // Signal and other actions
    emit testSignal(value);
    // ...

    Application won't use qml, but inherits some js functions from another project.

  • Hi,
    You can send C++ callback function as an argument. Firstly, use QScriptEngine globalObject method to get QObject callback (your testCallback) and then use your test function with testCallback parameter.

        QScriptValue global = engine.globalObject();
        QScriptValue testCallback ="Test").property("testCallback");
        QScriptValue test ="test");
        QScriptValueList vals;
        QScriptValue result =,vals);
        qDebug()<<"result : "<<result.toInt32()<<endl;

Log in to reply