C/C++与 Python 混合编程(3):Qt 调用嵌入python参数传递

1. 参数传递

参数传递的意思就是将 C 的值传递给 py 的函数,然后进行计算输出。

2. 例子

第二个程序的目标是在Python脚本中执行一个函数,现在这里需要传递参数。与关于非常高级接口的部分一样,Python解释器并不直接与应用程序交互(但这将在下一节中进行更改)。

运行Python脚本中定义的函数的代码是:

#define PY_SSIZE_T_CLEAN
#include 
#include 
int main(int argc, char *argv[])
{
 QCoreApplication a(argc, argv);
 PyObject *pName, *pModule, *pFunc;
 PyObject *pArgs, *pValue;
 int i;
 if (argc < 3) {
 fprintf(stderr,"用法: call pythonfile funcname [args]
");
 return 1;
 }
 Py_Initialize(); //初始化 Py 解释器
 PyRun_SimpleString("import sys
");
 PyRun_SimpleString("print(sys.path.append('/Users/wangxinnian/Downloads/qtApp/testQP2'))
");
 pName = PyUnicode_DecodeFSDefault(argv[1]);
 /* Error checking of pName left out */
 pModule = PyImport_Import(pName);
 Py_DECREF(pName);
 if (pModule != nullptr) {
 pFunc = PyObject_GetAttrString(pModule, argv[2]);
 /* pFunc is a new reference */
 if (pFunc && PyCallable_Check(pFunc)) {
 pArgs = PyTuple_New(argc - 3);
 for (i = 0; i < argc - 3; ++i) {
 pValue = PyLong_FromLong(atoi(argv[i + 3]));
 if (!pValue) {
 Py_DECREF(pArgs);
 Py_DECREF(pModule);
 fprintf(stderr, "不能转化参数
");
 return 1;
 }
 /* pValue reference stolen here: */
 PyTuple_SetItem(pArgs, i, pValue);
 }
 pValue = PyObject_CallObject(pFunc, pArgs);
 Py_DECREF(pArgs);
 if (pValue != nullptr) {
 printf("Result of call: %ld
", PyLong_AsLong(pValue));
 Py_DECREF(pValue);
 }
 else {
 Py_DECREF(pFunc);
 Py_DECREF(pModule);
 PyErr_Print();
 fprintf(stderr,"Call failed
");
 return 1;
 }
 }
 else {
 if (PyErr_Occurred())
 PyErr_Print();
 fprintf(stderr, "Cannot find function "%s"
", argv[2]);
 }
 Py_XDECREF(pFunc);
 Py_DECREF(pModule);
 }
 else {
 PyErr_Print();
 fprintf(stderr, "Failed to load "%s"
", argv[1]);
 return 1;
 }
 if (Py_FinalizeEx() < 0) {
 return 120;
 }
 return a.exec();
}

3.添加py 文件multiply.py

# This Python file uses the following encoding: utf-8
# if__name__ == "__main__":
# pass
def multiply(a,b):
 print("Will compute", a, "times", b)
 c = 0
 for i in range(0, a):
 c = c + b
 return c

4.计算结果

$ testQP2 multiply multiply 5 6

None

Will compute 5 times 6

Result of call: 30

5 总结

虽然该程序的功能相当大,但是大部分代码用于Python和C之间的数据转换以及错误报告。

初始化解释器之后,使用PyImport_Import()加载脚本。这个例程需要一个Python字符串作为参数,它是使用PyUnicode_FromString()数据转换例程构造的。

加载脚本之后,使用PyObject_GetAttrString()检索我们要查找的名称。如果名称存在,并且返回的对象是可调用的,则可以安全地假设它是一个函数。然后程序按照正常方式构造一个参数元组。然后用以下方法调用Python函数。

在函数返回时,pValue要么为NULL,要么包含对函数返回值的引用。请确保在检查值之后释放引用。

展开阅读全文

页面更新:2024-06-16

标签:参数   初始化   字符串   应用程序   脚本   函数   例程   例子   加载   接口   定义   名称   代码   程序   数据   科技

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号

Top