Windows下QT中用C++调用Python之一 - 基础调用

如需转载请标明出处:http://blog.csdn.net/itas109 

QQ技术交流群:129518033

 

由于Python简单而强大的功能,如果我们能在C++中集成Python那就太好了。

 

环境:

Python35:python-3.5.4  32位

QT:5.6.2 32位

编译器:Visual Studio 2013

操作系统:windows 7 64Bit SP1

 

1.Python的安装

略过

假设安装路径为C:\Python

 

2.Python头文件和库文件的提取

在路径C:\Python\Python35-32中有include和libs,里面分别是头文件和lib文件,在根目录下有dll文件。

 

3.在QT中集成Python

      3.1 在Pro中增加外部库的路径

注意:路径需要和自己的步骤2中提出的路径相同

unix|win32: LIBS += -L$$PWD/../../thirdBaseLib/Python35/x86/libs/ -lpython35

INCLUDEPATH += $$PWD/../../thirdBaseLib/Python35/include
DEPENDPATH += $$PWD/../../thirdBaseLib/Python35/include

 

 

3.2 引入Python.h头文件

 

#include "Python.h"

 

 

 

3.3 简单的运行Python

 

hello.py文件内容:

# -*- coding: utf-8 -*-

def hello():
    print("hello python!")

 

 

3.3.1 初始化Python模块

 

//初始化python模块
    Py_Initialize();
    if ( !Py_IsInitialized() )
    {
        return;
    }

 

 

3.3.2 设置*.py文件所在的路径

    // 将Python工作路径切换到待调用模块所在目录,一定要保证路径名的正确性
    PyRun_SimpleString("import sys");
    QString setSysPath = QString("sys.path.append('%1')").arg(QCoreApplication::applicationDirPath());
    PyRun_SimpleString(setSysPath.toStdString().c_str());

 

3.3.3 导入hello.py模块

//导入hello.py模块
    PyObject* pModule = PyImport_ImportModule("hello");
    if (!pModule) {
        infoData = "Can not open python file!";
        qDebug() << infoData;
        return;
    }

 

3.3.4获取hello模块中的hello函数

 

 

    //获取hello模块中的hello函数
    PyObject* pFunhello= PyObject_GetAttrString(pModule,"hello");
    if(!pFunhello){
        infoData = "Get function hello failed";
        qDebug()<< infoData;
        return;
    }

 

3.3.5调用hello函数

 

 

 

    //调用hello函数
    PyObject_CallFunction(pFunhello,NULL);

 

3.3.6结束,释放python

 

 

 

//结束,释放python
    Py_Finalize();

 

 

 

4.运行结果:

 

 

 

 

 

觉得文章对你有帮助,可以扫描二维码捐赠给博主,谢谢!


 如需转载请标明出处:http://blog.csdn.net/itas109 

QQ技术交流群:12951803

 

 

 

 

Windows 下使用 Qt C++ 调用 Python 有多种方法,以下是其中一种比较简单的方法: 1. 安装 Python 和 PyQt 首先,在 Windows 下安装 Python 和 PyQt。 2. 创建 Qt 项目 使用 Qt Creator 创建一个 Qt 项目,选择 C++ 应用程序。 3. 添加 Python 支持 在项目的 .pro 文件中添加以下内容: ``` CONFIG += link_pkgconfig PKGCONFIG += python-3.6 LIBS += -LC:/Python36/libs -lpython36 INCLUDEPATH += C:/Python36/include DEPENDPATH += C:/Python36/include ``` 其中,python-3.6 是你安装的 Python 版本号,C:/Python36 是 Python 的安装路径。 4. 创建 Python 脚本 在项目中创建一个 Python 脚本,例如 test.py,内容如下: ``` def add(a, b): return a + b ``` 5. 在 C++调用 PythonC++调用 Python 可以使用 Python.h 头文件和 Python 的 API。以下是一个简单的示例: ```cpp #include <Python.h> int main(int argc, char *argv[]) { Py_Initialize(); PyObject *pModule = PyImport_ImportModule("test"); if (pModule) { PyObject *pFunc = PyObject_GetAttrString(pModule, "add"); if (pFunc && PyCallable_Check(pFunc)) { PyObject *pArgs = PyTuple_New(2); PyTuple_SetItem(pArgs, 0, PyLong_FromLong(1)); PyTuple_SetItem(pArgs, 1, PyLong_FromLong(2)); PyObject *pResult = PyObject_CallObject(pFunc, pArgs); if (pResult) { long result = PyLong_AsLong(pResult); printf("result=%ld\n", result); Py_DECREF(pResult); } Py_DECREF(pArgs); } Py_DECREF(pFunc); } Py_DECREF(pModule); Py_Finalize(); return 0; } ``` 以上示例代码调用Python 脚本中的 add 方法,传入两个参数 1 和 2,输出结果 3。 注意事项: - 在调用Python API 后需要释放对象,避免内存泄漏。 - 实际项目中,可能需要使用 PyGILState_Ensure 和 PyGILState_Release 来保证线程安全。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

itas109

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值