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/
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值