参考文章:https://www.cnblogs.com/lgxZJ/archive/2017/12/31/8158132.html
Qt和JavaScript的交互
Qt提供了对JS的良好支持,有两种方式:
AScriptEngine- 4.3开始引入,现已被官方抛弃;
QJSEngine- 5.0引入;
- 封装了
V8引擎;
Qt中执行脚本
QJSValue QJSEngine::evaluate(const QString &program, const QString &fileName = QString(), int lineNumber = 1);
program:脚本代码
fileName/lineNumber:出错的时候包含在出错信息里
示例:
function test(){
return "123"
}
test();
QFile file("debug/JSTest.js");
if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
return;
}
QString js_code = file.readAll();
file.close();
QJSEngine engine;
QJSValue result = engine.evaluate(js_code); //执行脚本
QString str_result = result.toString(); //"123"
Qt对脚本的动态控制
Qt中执行脚本,是将脚本代码组成字符串,借此,可以动态控制脚本的代码逻辑
QString js_code = QString("%1/%2").arg(10).arg(2);
qDebug()<<js_code; //10/2
QJSValue result = engine.evaluate(js_code);
qDebug()<<result.toString(); //5
配置JS的全局变量
QJSValue QJSEngine::globalObject() const; Returns this engine's Global Object.void QJSValue::setProperty(const QString &name, const QJSValue &value); Sets the value of this QJSValue's property with the given name to the given value.
通过globalObject()方法获取引擎的全局对象,再使用setProperty()方法设置全局属性,该属性可以在js脚本中使用。
Qt的脚本化
QJSValue QJSEngine::newQObject(QObject *object); Creates a JavaScript object that wraps the given QObject object, using JavaScriptOwnership. Signals and slots, properties and children of object are available as properties of the created QJSValue.
使用newQObject函数,将Qt类封装成js对象,集成在js的引擎中。
Qt的信号槽、属性和子对象都可以封装。
将Qt的类封装起来,再通过全局属性将其传给js脚本,可以实现js和Qt的交互。
示例:
edit.setText("This is test");
QFile file("debug/JSTest.js");
if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
return;
}
QString js_code = file.readAll();
file.close();
QJSEngine engine;
engine.globalObject().setProperty("edit", engine.newQObject(ui->lineEdit));
QJSValue result = engine.evaluate(js_code); //执行脚本
将ui->lineEdit控件封装并传给js,在脚本中调用,运行后,界面的lineEdit控件上会出现This is test文字。