Qt C ++ - 如何在qjsengine中定义自定义函数 [英] Qt C++ - how to define custom functions in qjsengine
本文介绍了Qt C ++ - 如何在qjsengine中定义自定义函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用QJSEngine制作一个计算器,我需要像sqrt,pow,ln,sin等函数。在QScript中,您可以使用几行简单的代码定义自定义函数,但我不知道如何才能在QJSEngine实现这一目标。
我尝试过:
我我已经创建了一个js脚本,其中包含了我需要的所有功能,希望将它连接到引擎,但我不确定它是否可行,或者(如果是)我该怎么做。
I'm trying to make a calculator using QJSEngine and I need functions like sqrt, pow, ln, sin etc. In QScript you could define custom functions with a few simple lines of code, but I have no idea how I can achieve this in QJSEngine.
What I have tried:
I've created a js script with all the functions I need in hope of connecting it to the engine, but I'm not sure if it's possible, or (if it is) how can I do it.
推荐答案
根据 QJSEngine Class中的文档和示例Qt QML 5.11 [ ^ ]你可以将函数作为字符串传递给evaluate()
并执行返回 call()方法http://doc.qt.io/qt-5/qjsvalue.html\">QJSValue Class | Qt QML 5.11 [ ^ ]:
According to the documentation and the examples at QJSEngine Class | Qt QML 5.11[^] you can pass a function as string toevaluate()
and execute thecall()
method of the returned QJSValue Class | Qt QML 5.11[^]:
QJSEngine myEngine;
QJSValue fun = myEngine.evaluate("(function(a, b) { return a + b; })");
QJSValueList args;
args << 1 << 2;
QJSValue threeAgain = fun.call(args);
这篇关于Qt C ++ - 如何在qjsengine中定义自定义函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文