qt,script
qt 与 js 交互:
QFile file("./s.js"); if (!file.open(QIODevice::ReadOnly)) { qDebug() << "open error!"; exit(0); } QTextStream in(&file); in.setCodec("UTF-8"); QString str = in.readAll(); qDebug() << qApp->applicationDirPath(); qDebug() << str; file.close(); QScriptEngine e; QScriptValue v = e.evaluate(str); if (e.hasUncaughtException()) { qDebug() << "error!" << endl; } QScriptValue global = e.globalObject(); QScriptValue obj = global.property("obj"); qDebug() << obj.property("txt").toString(); QScriptValue h = e.newQObject(new Holder);// Holder 为QObject,动态属性 供 脚本引擎访问 qDebug() << "before s obj:" << h.property("str").toString(); obj.setProperty("holder", h); qDebug() << h.property("str").toString(); obj.property("run").call(obj); qDebug() << obj.property("txt").toString(); qDebug() << h.property("str").toString();
var obj = Object; obj.txt = "action1"; obj.run = function() { this.txt = "chang this.txt"; this.holder.str = "this.holder.text.toUpperCase()"; };