Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Calling JavaScript function from C++ ?
Forum Update on Monday, May 27th 2025

Calling JavaScript function from C++ ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 1 Posters 704 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SPlatten
    wrote on 4 Jan 2021, 09:47 last edited by SPlatten 1 Apr 2021, 09:47
    #1

    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.

    Kind Regards,
    Sy

    S 1 Reply Last reply 4 Jan 2021, 09:54
    0
    • S SPlatten
      4 Jan 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-c

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

      S Offline
      S Offline
      SPlatten
      wrote on 4 Jan 2021, 09:54 last edited by
      #2

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

      Kind Regards,
      Sy

      1 Reply Last reply
      0

      2/2

      4 Jan 2021, 09:54

      • Login

      • Login or register to search.
      2 out of 2
      • First post
        2/2
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved