找回密码
 立即注册
收起左侧

Qt4.8,6与python交互

1
回复
3862
查看
[复制链接]

尚未签到

来源: 2015-7-16 11:02:08 显示全部楼层 |阅读模式

马上注册,查看详细内容!注册请先查看:注册须知

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
我想在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这个头文件,请教各位为什么,或者说我的方法从根本上就错了,有没有其他解决方法
回复

使用道具 举报

尚未签到

2015-7-16 12:37:00 显示全部楼层
求教啊,各位。。。。。。。。。。。。。。。。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

公告
可以关注我们的微信公众号yafeilinux_friends获取最新动态,或者加入QQ会员群进行交流:190741849、186601429(已满) 我知道了