QT脚本学习笔记

阅读更多
QT脚本学习笔记

---》QT执行脚本
QScriptEngine myEngine;
QScriptValue three = myEngine.evaluate("function test(){var a=5;return a} test();");
qDebug()<
---》QT对象到脚本对象转换过程(js访问QT对象)
qt对象--》QScriptValue(qt包装对象)--》脚本对象

#include
#include

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QTextCodec::setCodecForTr(QTextCodec::codecForLocale());
QLabel label("xxx");
QScriptEngine myEngine;
QScriptValue scriptLabel= myEngine.newQObject(&label);//生成qt对象的包装对象
myEngine.globalObject().setProperty("myLabel", scriptLabel);//在脚本全局中声明myLable对象,把qt的包装对象关联到脚本对象
myEngine.evaluate("myLabel.setText('yyyy');");//在脚本中改变属性
qDebug()< return app.exec();
}

---》通过信号槽使用 js 中的函数

//QString fun = "(function(){print('hello world')})";  //方式一
QString fun = "f = function (){print('hello world')}"; //方式二
QScriptEngine myEngine;
QScriptValue sctiptFun = myEngine.evaluate(fun);

QPushButton button("click");
qScriptConnect(&button,SIGNAL(clicked()),myEngine.globalObject(),sctiptFun);
button.show();

---》qt直接调用js中的方法
QString script = "var obj = {test :function(){print('hello world');}}; ";
QScriptEngine myEngine;
myEngine.evaluate(script);
QScriptValue global = myEngine.globalObject();
QScriptValue fun = global.property("obj").property("test");
fun.call(global);

---》js中连接信号与槽
QScriptEngine myEngine;
//QString scriptStr("function test(){print('hello world');} btn.clicked.connect(this,test) ");
QString scriptStr("var obj = {test :function(){print('hello world');}}; btn.clicked.connect(obj,'test') ");
QPushButton button("click");
QScriptValue btn = myEngine.newQObject(&button);
myEngine.globalObject().setProperty("btn",btn);
myEngine.evaluate(scriptStr);
button.show();

参考:http://blog.163.com/jx_yp/blog/static/119704459201111145400203/
http://blog.163.com/jx_yp/blog/static/119704459201111145411698/
http://blog.163.com/jx_yp/blog/static/11970445920111114542128/

你可能感兴趣的:(qt,脚本)