Qt5(C++)调用python3脚本(带输入输出参数)

本例中的环境: Qt 5 9.0(编译器为32位mingw或32位msvc均可),python3.5.4 -32bit

1 前期准备

1.1 准备测试用的python文件(取名不要为test.py即可

def show():
    print("hello!")
def sum(a,b):
    return (a+b)

def main():
    show()
    print(sum(1,2))
 
if __name__ == '__main__':
    main()

先使用python自带的IDLE运行一下,保证代码无误
在这里插入图片描述

1.2 创建一个空的qt项目,选择release构建方式,保证其运行无误。然后将准备好的python文件拷贝在工程目录中(和.exe平行)

在这里插入图片描述

2 将python库文件路径导入qt项目中

展示一下我的python库文件:
在这里插入图片描述
在这里插入图片描述
导入:
注意:看一下我的libs文件夹中并没有 -lpython35这个文件,但是也要这样写才行,请大家参照进行填写
在这里插入图片描述

3 开始调用python

main.cpp

#include <QApplication>
#include <QDebug>
#include <QString>

//解决 Python 和 Qt 的关键词 slots 冲突
#pragma push_macro("slots")
#undef slots
#include <Python.h>
#pragma pop_macro("slots")

using namespace std;

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

    //如果初始化失败,返回
    if(!Py_IsInitialized())
    {
        qDebug()<<"Python init fail!";
        return a.exec();
    }
    //设置python文件路径
    PyRun_SimpleString("import sys");
    PyRun_SimpleString("sys.path.append('./')");

    //加载test.py文件
    PyObject *pModule = PyImport_ImportModule("testfile"); //注意python文件不要取名为test.py,会与python库文件冲突
    if(!pModule)
    {
        qDebug()<<"load pModule(testfile.py) fail!";
        return a.exec();
    }
    //加载函数show
    PyObject* pFun_show= PyObject_GetAttrString(pModule,"show");
    if(!pFun_show)
    {
        qDebug()<<"Get pFun_show(show) failed!";
        return a.exec();
    }
    //加载函数sum
    PyObject* pFun_sum= PyObject_GetAttrString(pModule,"sum");
    if(!pFun_sum)
    {
        qDebug()<<"Get pFun_sum(sum) failed!";
        return a.exec();
    }


    //调用函数show
    PyEval_CallObject(pFun_show,NULL);


    //调用函数sum
    PyObject* args=PyTuple_New(2);
    //【例 1】:输入参数:int  输出参数:int
    int iin1=1,iin2=2;  //输入参数赋值
    PyTuple_SetItem(args,0,Py_BuildValue("i",iin1));
    PyTuple_SetItem(args,1,Py_BuildValue("i",iin2));
    PyObject* pReturn1=PyEval_CallObject(pFun_sum,args);
    if(PyLong_Check(pReturn1))
    {
        int iout =PyLong_AsLong(pReturn1);
        qDebug()<<iout;  //打印输出
    }
    else
        qDebug()<<"return type is not Long!";

    //【例 2】:输入参数:float  输出参数:float
    float fin1=1.1,fin2=2.2;  //输入参数赋值
    PyTuple_SetItem(args,0,Py_BuildValue("f",fin1));
    PyTuple_SetItem(args,1,Py_BuildValue("f",fin2));
    PyObject* pReturn2=PyEval_CallObject(pFun_sum,args);
    if(PyFloat_Check(pReturn2))
    {
        float fout=PyFloat_AsDouble(pReturn2);
        qDebug()<<fout;  //打印输出
    }
    else
        qDebug()<<"return type is not Float!";

    //【例 3】:输入参数:QString  输出参数:QString
    QString strin1="Sun",strin2="tian"; //输入参数赋值
    PyTuple_SetItem(args,0,Py_BuildValue("s",strin1.toStdString().c_str()));
    PyTuple_SetItem(args,1,Py_BuildValue("s",strin2.toStdString().c_str()));
    PyObject* pReturn3=PyEval_CallObject(pFun_sum,args);
    if(PyUnicode_Check(pReturn3)){
        char* s=PyUnicode_AsUTF8(pReturn3);
        QString strout=QLatin1String(s);
        qDebug()<<strout;  //打印输出
    }
    else
        qDebug()<<"return type is not Unicode!";

    Py_Finalize(); //释放python
    return a.exec();
}

输出截图:
在这里插入图片描述

4 其中的坑:

(1)#include <Python.h>报错

Python\Python35-32\include\object.h:445: error: expected unqualified-id before ‘;’ token
PyType_Slot slots; / terminated by slot==0. */
原因: Python 和 Qt 的关键词 slots 冲突
解决:
#pragma push_macro(“slots”)
#undef slots
#include <Python.h>
#pragma pop_macro(“slots”)

(2)PyImport_ImportModule返回值为空

原因: py文件错误
解决:将python文件放在和qt可执行文件(.exe)平行的位置

(3)release\callPy_test.exe: -1: error: LNK1120: 15 个无法解析的外部命令

可能原因: python和qt编译器位数不对应
解决:采用位数一致的编译器(对于qt来说,和qt位数无关,只和编译器位数有关)

(4)要保证python文件自己运行不报错,否则qt调用会造成崩溃

(5)python文件不能取名为test.py

后记:使用Qt(c++)和python进行混合编程,可以发挥c++的速度优势和python的灵活和便捷优势,也可以使系统更好的实现模块化,是一种值得研究的技术。

发布了18 篇原创文章 · 获赞 5 · 访问量 6678

猜你喜欢

转载自blog.csdn.net/Sun_tian/article/details/104328564