Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/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:

    {"decoder":"simon.js@decodeFIOrdy"
      ,"match":"mdFileIO"
    , "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() ) {
            return;
        }
        QString strDecoder = citrDecoder.value().toString()
               ,strMatch = citrMatch.value().toString()
               ,strMember = citrMember.value().toString();
        if ( strDecoder.isEmpty() == true
          || strMatch.isEmpty() == true
          || strMember.isEmpty() == true ) {
            return;
        }
        QStringList slstDecoder = strDecoder.split(clsScriptHelper::msccDecoderDelimiter);
    
        if ( slstDecoder.length() != SDA_COUNT ) {
            return;
        }
        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:
    https://forum.qt.io/topic/67039/calling-js-functions-in-loaded-components-from-c

    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