Important: Please read the Qt Code of Conduct -

Calling JavaScript function from C++ ?

  • From JavaScript I can call a C++ routine, I've done this already, now what I want to do is be able to call a JavaScript routine from C++. I have a C++ routine called:

    Q_INVOKABLE void registerDecoder(const QJsonObject& crobjJSON);

    In the passed constant reference JSON passed I have:

    , "member":"source"}

    In my C++ routine:

    void clsScriptHelper::registerDecoder(const QJsonObject& crobjJSON) {
        QJsonObject::const_iterator citrDecoder = crobjJSON.find(clsJSON::mscszDecoder)
                                   ,citrMatch = crobjJSON.find(clsJSON::mscszMatch)
                                   ,citrMember = crobjJSON.find(clsJSON::mscszMember);
        if ( citrDecoder == crobjJSON.end()
          || citrMatch == crobjJSON.end()
          || citrMember == crobjJSON.end() ) {
        QString strDecoder = citrDecoder.value().toString()
               ,strMatch = citrMatch.value().toString()
               ,strMember = citrMember.value().toString();
        if ( strDecoder.isEmpty() == true
          || strMatch.isEmpty() == true
          || strMember.isEmpty() == true ) {
        QStringList slstDecoder = strDecoder.split(clsScriptHelper::msccDecoderDelimiter);
        if ( slstDecoder.length() != SDA_COUNT ) {
        QString strFile = slstDecoder[SDA_FILE]
               ,strFunction = slstDecoder[SDA_FUNCTION];

    How do I call the decoder function which is specified in the JSON from C++ ? strFile contains the script file name, strFunction contains the function name.

    I found this:

    And am in the process of seeing if I can use it.

  • I think I've resolved this by calling the instance of QJSEngine my application is using and calling the evaluate function.

Log in to reply