QT C++ Call C++ function in QJSEngine function
-
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?
@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. -
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?
@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++? -
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.