首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 企业软件 > 行业软件 >

QT脚本学习札记

2013-07-11 
QT脚本学习笔记QT脚本学习笔记---》QT执行脚本 QScriptEngine myEngine QScriptValue three myEngine.ev

QT脚本学习笔记
QT脚本学习笔记

---》QT执行脚本
QScriptEngine myEngine;
QScriptValue three = myEngine.evaluate("function test(){var a=5;return a} test();");
qDebug()<<three.toNumber();//5

---》QT对象到脚本对象转换过程(js访问QT对象)
qt对象--》QScriptValue(qt包装对象)--》脚本对象

#include <QtGui>
#include <QtScript>

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()<<label.text();//yyyy
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/

热点排行