步骤一
根据自己的需要打开QT,新建项目,然后编辑.pro文件
INCLUDEPATH += C:\Users\Administrator\AppData\Local\Programs\Python\PythonXX\include
LIBS += -LC:\Users\Administrator\AppData\Local\Programs\Python\PythonXX\libs -lpythonXX
将上面两行代码写入.pro文件,XX的位置换成自己的python版本即可
步骤二
测试:
在.cpp文件中输入头文件测试:
#include<Python.h>
报错
我这边显示有两个错误,如下所示:
错误一:
:error: expected unqualified-id before ';' token
PyType_Slot *slots; /* terminated by slot==0. */
^
错误二:
error: '::hypot' has not been declared
using ::hypot;
网上百度了一下好像是Python.h中的一个方法和cmath中的方法重名所以会有这样的错误,把代码改成如下所示的情况就可以解决。
#include "model.h"
#include "ui_model.h"
#include <cmath>
#define _hypot hypot
#undef slots
#include<Python.h>
也就是在#include<Python.h>之前加上#include 、#define _hypot hypot、#undef slots即可解决