2013-09-30 57 views
0

我有一個嵌入式系統,其GUI是使用html開發的。裏面,HTML,我寫javascripts。現在,我如何使用QT從Javascript調用C++函數 - 中間件?我聽說QT是從Javascript調用C++函數的最簡單方法。有沒有任何例子,從JAVA SCRIPT調用C++函數。我對python完全陌生,python是否提供這種功能?例子對我有幫助。請通過示例來解釋我。此外,內部機制 - 從Javascript調用C++將會很有幫助。如果我聽起來很奇怪,請忽略我的python部分。因爲我對python完全陌生。我真的沒有它的力量。如何使用QT/python從Javascript調用C++函數?

比方說,我有CPP中的中間件 - 爲了演示目的只有一個文件。

#include "stdio.h" 

class Sample 
{ 

private: 

    int net_value; 


public: 


    Sample() 
    { 

    printf("constructor called\r\n"); 

    } 



void set_value() 
    { 
    net_value = 10; 

    } 


}; 

我已編譯如下。

GCC -c -fPIC sample.cpp的

GCC --shared sample.o -o libmysample.so

現在,我的HTML文件。

<html> 
<head> 
    <script type="text/javascript"> 
function calculate() 
{ 
alert('You clicked the top text'); 
} 

function functionTwo() 
{ alert('You clicked the bottom text'); 

} 
    </script> 
</head> 
<body> 

<form action="http://136.170.195.17/cgi-bin/jordan_cgi.cgi"> 
<h1> Enter the First Equation </h1> 
<input type=text name=val1 size=1> 
X 

+ 
<input type=text name=val2 size=1> 
Y 

+ 
<input type=text name=val3 size=1> 
Z 
= 
<input type=text name=val4 size=1> 
<h1> Enter the Second Equation </h1> 
<input type=text name=val11 size=1> 
X 

+ 
<input type=text name=val12 size=1> 
Y 

+ 
<input type=text name=val13 size=1> 
Z 
= 
<input type=text name=val14 size=1> 

<h1> Enter the Third Equation </h1> 
<input type=text name=va2l1 size=1> 
X 

+ 
<input type=text name=va2l2 size=1> 
Y 

+ 
<input type=text name=va2l3 size=1> 
Z 
= 
<input type=text name=va2l4 size=1> <br> 
<br> 
<div><input type="submit" value="Compute!"></div> 
</form> 

</body> 
</html> 

現在,我的興趣是從JAVASCRIPT函數調用void set_value()。

方程計算(){

/** First instantiate Sample class and then invoke the set_value 
---> Here I want to invoke set_value() -> remember the set_value is in the 
.so file. **/ 

alert('You clicked the top text'); 
} 

我該怎麼辦實現上述任務?這個例子可以進一步詳述回調部分。這樣,調用C++函數來註冊回調和其他函數變得非常方便。請舉例說明這一點?

回答

0

在javascript中使用內聯C++代碼,在文件cpp2js.js中提供了一個方法cpp2js(code {,callback})。包括在您的網頁文件:

<script type='text/javascript' src="cpp2js.js"></script> 

Inline C++ functions in Javascript

+0

所以,如果我有一個在HTML/Java腳本的所有C++的功能和UI中間件。那麼我需要在中間件庫上運行cpp2js工具還是需要運行此工具?因爲我必須將中間件中的C++函數與UI相鏈接。 –

+0

cpp2js是您如何實現的示例 - 瞭解您將如何打電話。你還沒有發佈任何代碼。我的最後幾乎沒有什麼消息。但關鍵是要在* .js文件中編寫cpp代碼,在您的Html代碼中將它作爲