1、安装python并查看python安装路径,配置qt的pro文件
sudo apt-get install python-dev否则#include <Python.h> 文件提示找不到
#-------------------------------------------------
#
# Project created by QtCreator 2018-11-07T17:01:22
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = untitled
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
DISTFILES += \
zytest.py
INCLUDEPATH += -I /usr/include/python2.7
LIBS += /usr/lib/i386-linux-gnu/libpython2.7.so.1
2、qt新建python测试文件
3、添加Python.h头文件,输入一下测试代码
Py_Initialize();
if (!Py_IsInitialized()) {
return;
}
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append('./')");
PyObject* pModule = PyImport_ImportModule("zytest");
if (!pModule) {
qDebug()<<"cant open python file";
return;
}
PyObject* pFunHello = PyObject_GetAttrString(pModule,"hello");
if (!pFunHello) {
qDebug("get function hello failed");
return;
}
PyObject_CallFunction(pFunHello,NULL);
Py_Finalize();
4、将之前建立的.py 文件放到可执行文件同级目录
5、运行显示调用成功
6、参考
http://www.cnblogs.com/shenghl/p/4442528.html
https://blog.csdn.net/realDonaldTrump/article/details/79949182