如何使用 QT/Python 从 Javascript 调用C++函数

how to invoke C++ functions from Javascript using QT/python?

本文关键字:Javascript 调用 C++ 函数 Python 何使用 QT      更新时间:2023-10-16

我有一个嵌入式系统,其GUI是使用html开发的。在里面,HTML,我正在编写javascript。现在,如何使用 QT 从 Javascript 调用C++函数中间件?我听说QT是从Javascript调用C++函数的最简单方法。有没有从 JAVA 脚本调用 C++ 函数的示例。我是python的新手,python提供此功能吗?例子确实对我有帮助。请通过示例解释我。此外,内部机制 - 从Javascript调用C++将很有帮助。请忽略我的蟒蛇部分,如果我听起来很奇怪。因为我对python完全陌生。我真的不知道它的力量。

假设我在 CPP 中有中间件 - 出于演示目的只有一个文件。

#include "stdio.h"
class Sample
{
 private:
   int net_value;

public:

  Sample()
  {
    printf("constructor calledrn");
  }

 void set_value()
  {
    net_value = 10;
  }

};

我将其编译如下。

GCC -c -fPIC 样品.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++ 函数来注册回调和其他函数就变得很方便了。请举例说明这一点?

在javascript中使用内联C++代码,在文件cpp2js.js中提供了一个方法cpp2js(code {,callback}(。在网页中包含此文件:

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

JavaScript 中的内联C++函数