环境准备
QT版本:4.8.5 下载地址: http://download.qt.io/archive/qt/4.8/4.8.5/

MinGW编译器:MinGW 32位 下载地址: https://osdn.net/projects/mingw/releases/

python版本:2.7 32位 下载地址: https://www.python.org/downloads/windows/

1、引用python库
pro 文件加入以下内容,其它版本可以尝试直接添加外部库

本机python安装目录:D:/Python1/Python27-32/

INCLUDEPATH += -I D:/Python1/Python27-32/include
LIBS += -LD:/Python1/Python27-32/libs/ -lpython27

2、添加python文件
Qt调用python有参和无参方法,取方法返回值_调用函数

#!/usr/bin/env python
# -*- coding: utf-8 -*-

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

def show(str):
return str

def add(a, b):
return a + b

3、调动无参无返回值方法

// 导入 fun1.py 模块
PyObject* fun1Py = PyImport_ImportModule("fun1");

// 取fun1.py的hello方法
PyObject* fhello = PyObject_GetAttrString(fun1Py,"hello");
// 调用函数
PyObject* fhelloc1 = PyObject_CallObject(fhello, NULL);
PyObject* fhelloc2 = PyObject_CallFunction(fhello, NULL);
PyObject* fhelloc3 = PyObject_CallFunctionObjArgs(fhello, NULL, NULL);

4、调用QString类型参数返回QString类型的方法

// 取fun1.py的show方法
PyObject* fshow = PyObject_GetAttrString(fun1Py,"show");
// 创建元组设置参数
PyObject* arg1 = PyString_FromString("perfect");
PyObject* args = PyTuple_New(1);
PyTuple_SetItem(args, 0, arg1);

PyObject* fshowc = PyObject_CallObject(fshow, args);
if(fshowc) {
QString result = PyString_AsString(fshowc);
qDebug() << "result:" << result;
}

5、调用long类型参数返回long类型的方法

// 取fun1.py的add方法
PyObject* fadd = PyObject_GetAttrString(fun1Py,"add");
// 创建元组设置参数
PyObject* args = PyTuple_New(2);
PyObject* arg1 = PyLong_FromLong(11111);
PyObject* arg2 = PyLong_FromLong(22222);
PyTuple_SetItem(args, 0, arg1);
PyTuple_SetItem(args, 1, arg2);

// 调用函数
PyObject* faddc = PyObject_CallObject(fadd, args);
if (faddc) {
long result = PyLong_AsLong(faddc);
qDebug() << "result:" << result;
}

6、全部代码

#include <QCoreApplication>
#include <QtDebug>
#include <Python.h>

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);

qDebug() << "==========start============";

// 初始化python环境
Py_Initialize();
if(!Py_IsInitialized()){
qDebug()<<"Py_Initialize fail!";
}

PyRun_SimpleString("print 'this is python print'");

PyObject* args;
PyObject* arg1;
PyObject* arg2;

// 导入 fun1.py 模块
PyObject* fun1Py = PyImport_ImportModule("fun1");

// 取fun1.py的hello方法
PyObject* fhello = PyObject_GetAttrString(fun1Py,"hello");
// 调用函数
// PyObject* fhelloc = PyObject_CallObject(fhello, NULL);
// PyObject* fhelloc = PyObject_CallFunction(fhello, NULL);
PyObject* fhelloc = PyObject_CallFunctionObjArgs(fhello, NULL, NULL);

// 取fun1.py的show方法
PyObject* fshow = PyObject_GetAttrString(fun1Py,"show");
// 创建元组设置参数
arg1 = PyString_FromString("perfect");
args = PyTuple_New(1);
PyTuple_SetItem(args, 0, arg1);

PyObject* fshowc = PyObject_CallObject(fshow, args);
if(fshowc) {
QString result = PyString_AsString(fshowc);
qDebug() << "result:" << result;
}

// 取fun1.py的add方法
PyObject* fadd = PyObject_GetAttrString(fun1Py,"add");
// 创建元组设置参数
args = PyTuple_New(2);
arg1 = PyLong_FromLong(11111);
arg2 = PyLong_FromLong(22222);
PyTuple_SetItem(args, 0, arg1);
PyTuple_SetItem(args, 1, arg2);

// 调用函数
PyObject* faddc = PyObject_CallObject(fadd, args);
if (faddc) {
long result = PyLong_AsLong(faddc);
qDebug() << "result:" << result;
}

Py_Finalize();
qDebug() << "==========end============";

return a.exec();
}

7、运行效果
Qt调用python有参和无参方法,取方法返回值_开发语言_02
原文:https://blog.51cto.com/StarBigData/3643691