|
我想在Qt的c++程序里与python交互,就是调用python编写的接口函数,比如python里有一个hello函数:
def hello()
print "hello"
然后在Qt的main.cpp中
#include <QCoreApplication>
#include <Python.h>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
//初始化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");
//注释掉的这部分是另一种获得test模块中的hello函数的方法
// PyObject* pDict = PyModule_GetDict(pModule);
// if (!pDict) {
// printf("Cant find dictionary.\n");
// return -1;
// }
// PyObject* pFunhello = PyDict_GetItemString(pDict, "hello");
if(!pFunhello){
cout<<"Get function hello failed"<<endl;
return -1;
}
//调用hello函数
PyObject_CallFunction(pFunhello,NULL);
//结束,释放python
Py_Finalize();
return a.exec();
}
但是结果是Qt找不到Python.h这个头文件,我已经装了python-dev这个包,并且系统的/usr/include/python2.7这个目录里有Python.h这个头文件,请教各位为什么,或者说我的方法从根本上就错了,有没有其他解决方法
|
|