QT应用(12)-QT调用python

功能:用qtcreator调用python文件。
1.环境:win10       64 位
           python3.8 64位
           mingw      64位 位数一定要匹配,如果是64位,全部为64位。包括环境变量中的设定值。2.pro 配置    路径选择 自己盘上的安装路径

win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../../ProgramData/Python/Python38/libs/ -lpython38
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../../ProgramData/Python/Python38/libs/ -lpython38_d

INCLUDEPATH += $$PWD/../../ProgramData/Python/Python38/libs
DEPENDPATH += $$PWD/../../ProgramData/Python/Python38/libs
INCLUDEPATH += $$PWD/../../ProgramData/Python/Python38/include
DEPENDPATH += $$PWD/../../ProgramData/Python/Python38/include

#win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/../../ProgramData/Python/Python38/libs/libpython38.a
#else:win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/../../ProgramData/Python/Python38/libs/libpython38d.a
win32:!win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/../../ProgramData/Python/Python38/libs/python38.lib
else:win32:!win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/../../ProgramData/Python/Python38/libs/python38_d.lib

3.main.cpp

 1 #include "qtpyprj.h"
 2 
 3 #include 
 4 #include 
 5 #include
 6 #include
 7 
 8 int main(int argc, char *argv[])
 9 {
10     QApplication a(argc, argv);
11     qDebug()<<"run";
12     //设置了环境变量
13     Py_SetPythonHome((const wchar_t *)(L"D:/ProgramData/Python/Python38"));
14        //1.初始化Python解释器
15      Py_Initialize();
16      if(!Py_IsInitialized())
17      {
18           qDebug()<<"无法初始化Python解释器";
19            return -1;
20       }
21      //2.导入文件模块  py文件路径的问题
22      PyObject * pModule = PyImport_ImportModule("qtpyprj");          //pModule 调用的文件名,没有后缀
23      if (!pModule)
24      {
25             qDebug()<<"无法导入qtpyprj模块";
26             return -1;
27      }
28 
29      //3.调用函数无参的
30      PyObject * pFunc_run = PyObject_GetAttrString(pModule, "run"); //pFunc 调用的函数名
31      if (!pFunc_run || !PyCallable_Check(pFunc_run))
32      {
33           qDebug()<<"无法从qtpyprj模块中加载run函数";
34           return -1;
35      }
36      PyEval_CallObject(pFunc_run, NULL);
37 
38      /*qDebug()<<"开始调用add ";
39      PyObject * pFunc_add = PyObject_GetAttrString(pModule, "add"); //pFunc 调用的函数名
40      if (!pFunc_add || !PyCallable_Check(pFunc_add))
41      {
42         qDebug()<<"无法从qtpyprj模块中加载add函数";
43         return -1;
44      }
45 
46         //4. 函数调用是有两种,一种传参数的,一种不传参数的。
47         //传参数有两种方法:
48         //4.1.1创建方法1
49          PyObject *arg3 =PyTuple_New(2);
50          PyObject *arg1 = PyLong_FromLong(1050);
51          PyObject *arg2 = PyLong_FromLong(520);
52 
53          PyTuple_SetItem(arg3,0,arg1);
54          PyTuple_SetItem(arg3,1,arg2);
55 
56          //4.1.2创建方法2
57       //   PyObject *args3 =Py_BuildValue("",8.5,103.2);
58          //4.2 调用python 的add函数
59          PyObject *fe=PyEval_CallObject(pFunc_add, arg3);                      //pFunc 调用函数
60          //4.3返回值处理
61          float res2=0;
62          PyArg_Parse(fe,"f",&res2);//是浮点数
63 
64          //4.4 输出结果
65          qDebug()<<"add(f):"<

4.python 文件存放目录    .py,.pyc与.exe3个文件同文件夹。

QT应用(12)-QT调用python_第1张图片

 5.python文件

def run():
    print("hello qt")

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

你可能感兴趣的:(python,qt,os,tensorflow,windows)