c调用c++ qt_C/C++/Qt与 Python 混合编程(3):Qt 调用嵌入python

1. 参数传递

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

将数据值从C转换为Python,

使用转换后的值对Python接口例程执行函数调用

将数据值从Python调用转换为C。

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]\n");

return 1;

}

Py_Initialize(); //初始化 Py 解释器

PyRun_SimpleString("import sys\n");

PyRun_SimpleString("print(sys.path.append('/Users/wangxinnian/Downloads/qtApp/testQP2'))\n");

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, "不能转化参数\n");

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\n", PyLong_AsLong(pValue));

Py_DECREF(pValue);

}

else {

Py_DECREF(pFunc);

Py_DECREF(pModule);

PyErr_Print();

fprintf(stderr,"Call failed\n");

return 1;

}

}

else {

if (PyErr_Occurred())

PyErr_Print();

fprintf(stderr, "Cannot find function \"%s\"\n", argv[2]);

}

Py_XDECREF(pFunc);

Py_DECREF(pModule);

}

else {

PyErr_Print();

fprintf(stderr, "Failed to load \"%s\"\n", 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,要么包含对函数返回值的引用。请确保在检查值之后释放引用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值