Calling JavaScript function from C++ ?
-
wrote on 4 Jan 2021, 09:47 last edited by SPlatten 1 Apr 2021, 09:47
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-cAnd am in the process of seeing if I can use it.
-
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-cAnd am in the process of seeing if I can use it.
wrote on 4 Jan 2021, 09:54 last edited byI think I've resolved this by calling the instance of QJSEngine my application is using and calling the evaluate function.
1/2