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. QT C++ Call C++ function in QJSEngine function
Forum Update on Monday, May 27th 2025

QT C++ Call C++ function in QJSEngine function

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 490 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.
  • K Offline
    K Offline
    kronos
    wrote on last edited by
    #1

    Hello!
    So I'm trying to call a function implemented in C++ in my JS code, which gets evaluated with the QJSEngine.

    formula = "if(CH_53 > 0 || hasChannel(CH_155) ? CH_155 > 0 : false){​​​​​​​​return CH_2 - CH_138;}​​​​​​​​else{​​​​​​​​return nan;}​​​​​​​​";
    QJSValue formulaFunction = jsEngine->evaluate("(function(" + functionArgList.join(",") + "){ " + formula + "})");
    

    As shown here, I try to call the hasChannel function. At runtime I get the error "ReferenceError: hasChannel is not defined"
    I marked the function as Q_INVOKABLE but still.

    Am I completely misunderstanding something or should this work?

    KroMignonK 1 Reply Last reply
    1
    • K kronos

      Hello!
      So I'm trying to call a function implemented in C++ in my JS code, which gets evaluated with the QJSEngine.

      formula = "if(CH_53 > 0 || hasChannel(CH_155) ? CH_155 > 0 : false){​​​​​​​​return CH_2 - CH_138;}​​​​​​​​else{​​​​​​​​return nan;}​​​​​​​​";
      QJSValue formulaFunction = jsEngine->evaluate("(function(" + functionArgList.join(",") + "){ " + formula + "})");
      

      As shown here, I try to call the hasChannel function. At runtime I get the error "ReferenceError: hasChannel is not defined"
      I marked the function as Q_INVOKABLE but still.

      Am I completely misunderstanding something or should this work?

      KroMignonK Offline
      KroMignonK Offline
      KroMignon
      wrote on last edited by KroMignon
      #2

      @kronos said in QT C++ Call C++ function in QJSEngine function:

      Am I completely misunderstanding something or should this work?

      AFAIK, jsEngine->evaluate() enables you to execute JavaScript code from C++ code. But not call any C++ function from there!
      How should this be possible?!?
      What you can do, is call methods from a C++ object instance which is "inserted" to the engine context with jsEngine->globalObject().setProperty();
      See https://doc.qt.io/qt-5/qjsengine.html#qobject-integration for more details.

      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

      1 Reply Last reply
      1
      • K Offline
        K Offline
        kronos
        wrote on last edited by
        #3

        So I just have to create a class, implement the method there and then I can add the object as a global property and call it there?

        KroMignonK 1 Reply Last reply
        0
        • K kronos

          So I just have to create a class, implement the method there and then I can add the object as a global property and call it there?

          KroMignonK Offline
          KroMignonK Offline
          KroMignon
          wrote on last edited by
          #4

          @kronos said in QT C++ Call C++ function in QJSEngine function:

          So I just have to create a class, implement the method there and then I can add the object as a global property and call it there?

          Have you read the link a post in my previous message?
          You will find all information there.

          But why do you want to use a JavaScript engine to call a method from a C++ class.
          It is too simple and fast to do it directly in C++?

          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

          1 Reply Last reply
          1
          • K Offline
            K Offline
            kronos
            wrote on last edited by
            #5

            I get the formula via an API (this was only an example) and it can change pretty drastically, the JS Engine does the parsing for me (if statements, mathematical operations and so on) . So I just have to parse some arguments and then I'm fine.
            Since the C++ function is calling other c++ functions, I cannot implement it in JS once.

            1 Reply Last reply
            0

            • Login

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