QT 调用 python 模块
2018-01-08 16:33
1846 查看
QT 调用 python 模块
linux环境下 python2.7 Qt 5.10.01 testPy.pro
QT += core QT -= gui CONFIG += c++11 CONFIG += console CONFIG -= app_bundle TEMPLATE = app TARGET = testPy DESTDIR = $$PWD/../bin SOURCES += main.cpp DEFINES += QT_DEPRECATED_WARNINGS #-- add python2.7 lib-- INCLUDEPATH += -I /usr/include/python2.7 LIBS += -L /usr/lib/python2.7 -lpython2.7 #--test.py file OTHER_FILES += test.py\ #copy files { unix{ system(cp -rv $$OTHER_FILES $$DESTDIR) } }
2 main.cpp
#include <QCoreApplication> #include <Python.h> #include <QDebug> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); //初始化python模块 Py_Initialize(); if ( !Py_IsInitialized() ) { return -1; } //导入test.py模块 PyObject* pModule = PyImport_ImportModule("test"); if (!pModule) { qDebug("Cant open python file!\n"); return -1; } //获取test模块中的hello函数 PyObject* pFunhello= PyObject_GetAttrString(pModule,"hello"); //注释掉的这部分是另一种获得test模块中的hello函数的方法 // PyObject* pDict = PyModule_GetDict(pModule); // if (!pDict) { // qDebug("Cant find dictionary.\n"); // return -1; // } // PyObject* pFunhello = PyDict_GetItemString(pDict, "hello"); if(!pFunhello){ qDebug()<<"Get function hello failed"; return -1; } //调用hello函数 PyObject_CallFunction(pFunhello,NULL); //结束,释放python Py_Finalize(); return a.exec(); }
3 test.py
#!/usr/bin/env python def hello(): print("qt invoke python!")
相关文章推荐
- QT/C++ 下调用python自定义模块
- python调用模块&函数
- python下面通过ctypes模块调用c++库的方法
- linux 下Python调用C模块
- QT 下调用lua自定义模块
- Python3基础 创建一个模块 导入并调用其中的函数 主代码与模块代码在同一文件夹下
- Qt调用Python脚本
- ESL python调用C模块时传递unicode字符串报错问题解决
- Python 3.X 调用多线程C模块,并在C模块中回调python函数的示例
- 设置python 模块调用路径
- 深入浅析 C++ 调用 Python 模块
- 解决多个py模块调用同一个python的logging模块,打印日志冲突问题
- python调用自定义模块
- linux 下Python调用C模块
- Python 引用多个模块,调用模块中的函数时...要注意的地方
- C#调用Python脚本并使用Python的第三方模块
- c和python混编,c调用python模块中的函数
- python 删除某个路径下所有文件及模块调用注意事项
- python下面通过ctypes模块调用c++库的方法
- python re 模块及正则表达式调用认识 (2)