24,854
社区成员
发帖
与我相关
我的任务
分享
#include <QCoreApplication>
#include <Python.h>
#include <pythonrun.h>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
//PyRun_SimpleString("import sys");
char uc[20] = "C:\\Python27";
Py_SetPythonHome(uc);
//初始化python模块
Py_Initialize();
if ( !Py_IsInitialized() )
{
return -1;
}
//导入test.py模块
PyObject* pModule = PyImport_ImportModule("test");
if (!pModule) {
printf("Cant open python file!\n");
return -1;
}
//获取test模块中的hello函数
PyObject* pFunhello= PyObject_GetAttrString(pModule,"hello");
if(!pFunhello){
cout<<"Get function hello failed"<<endl;
return -1;
}
//调用hello函数
PyObject_CallFunction(pFunhello,NULL);
//结束,释放python
Py_Finalize();
return a.exec();
}