需求是這樣的:
需要給一個硬件接口測試些東西,用js來調用,js做成不同的獨立模塊分別測試不同的硬件接口,包括DMD內存,PCIE帶寬等等。需要用一個exe來載入這些js文件分別調用測試模塊。exe用Qt來做。就只能用QtScript模塊了么。
?
QtScript是一個解釋腳本的引擎 符合EMCAScript標準。
?
1 void test_js::ExecuteJsFile(){ 2 3 QScriptEngine engine; 4 5 if(JsFilePath.isEmpty()){ 6 qDebug() << "No JavaScript File, Please check it!" <<endl; 7 return ; 8 } 9 10 11 engine.installTranslatorFunctions(); 12 13 Interfaces pcie; //pcie public add slot or check lineedit settext 14 QScriptValue scriptButton = engine.newQObject(&pcie);//生成C++對象的代理腳本對象 15 engine.globalObject().setProperty("button", scriptButton);//將對象注冊到javascript 16 17 QString fileName(JsFilePath); 18 QFile scriptFile(fileName); 19 scriptFile.open(QIODevice::ReadOnly); 20 QTextStream stream(&scriptFile); 21 QString contents = stream.readAll(); 22 scriptFile.close(); 23 24 QScriptValue result = engine.evaluate(contents,fileName); 25 26 27 28 //parsing js format error 29 if(result.isError()) { 30 31 32 qDebug() << "Error File Name is " << fileName << endl; 33 qDebug() << "Error line number is" << result.property("lineNumber").toInt32() << endl; 34 qDebug() << "Error Details is " << result.toString() << endl; 35 return ; 36 } 37 38 qDebug() << fileName <<" 's Result: " << result.toInt32() <<endl; 39 40 }
opencv和qt結合編程、?
?
Interfaces 這個類的頭文件是這樣的:
1 #ifndef INTERFACES_H 2 #define INTERFACES_H 3 #include <QtCore/QObject> 4 class Interfaces : public QObject{ 5 Q_OBJECT 6 public slots: 7 void pcie_interface(); 8 }; 9 #endif
對應的實現文件:
1 #include <QtCore/QDebug> 2 #include "test_interfaces.h" 3 4 using namespace std; 5 6 void Interfaces::pcie_interface(){ 7 qDebug() << "called fuck!" << endl; 8 }
下面,javascript文件里面調用pcie_interface()函數,注意,以上pcie_interface函數必須聲明成槽函數,不然會報錯。
1 //helloscript.js 2 button.pcie_interface();
?
references:
基于Qt的編程框架。http://blog.csdn.net/guxch/article/details/7656846
http://blog.csdn.net/bbjjqq/article/details/6164291
http://blog.csdn.net/liuhongwei123888/article/details/6162159
?