![](/img/trans.png)
[英]How to invoke C++/Qt function from Javascript in QWebengine?
[英]how to invoke C++ functions from Javascript using QT/python?
我有一個嵌入式系統,其GUI使用html開發。 在HTML里面,我正在編寫JavaScript。 現在,如何使用QT從Java調用C ++函數-中間件? 我聽說QT是從Javascript調用C ++函數的最簡單方法。 有沒有示例可以從JAVA SCRIPT調用C ++函數。 我對python完全陌生,python是否提供此功能? 例子對我有幫助。 請通過示例向我解釋。 另外,內部機制-從Java腳本調用C ++將很有幫助。 如果我聽起來很奇怪,請忽略我的python部分。 因為我對python完全陌生。 我真的沒有力量。
假設,我在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()。
函數calculate(){
/** 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 ++函數注冊回調和其他函數就變得很方便。 請舉例說明嗎?
在JavaScript中使用內聯C ++代碼,文件cpp2js.js中提供了方法cpp2js(code {,callback})。 將此文件包含在您的網頁中:
<script type='text/javascript' src="cpp2js.js"></script>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.