QT C++ Call C++ function in QJSEngine function
-
wrote on 4 Aug 2021, 12:47 last edited by
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?
-
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?
wrote on 4 Aug 2021, 12:55 last edited by KroMignon 8 Apr 2021, 12:56@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 withjsEngine->globalObject().setProperty();
See https://doc.qt.io/qt-5/qjsengine.html#qobject-integration for more details. -
wrote on 4 Aug 2021, 13:04 last edited by
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?
-
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?
wrote on 4 Aug 2021, 13:18 last edited by@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++? -
wrote on 4 Aug 2021, 13:22 last edited by
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/5