I am creating a Qt Quick GUI application (for Windows) that uses OpenGL and C ++ for some intensive computing. I want to embed python code in an application to do some things that are relatively simpler in python.
Basically, I just want the C ++ code to call the function in a python script, and let the script do the job and then save the returned data in a variable (string, float, etc.) for later use, I use Qt creator, and I got python3 lib for MinGW compiler. I tried some code, but its like python lib is not quite compatible with the creator of Qt. Using pyqt for this would be a good idea? What would be the best and easiest way to do this?
EDIT: This is the base code I tried, at first it gave me an error message, can't find pyconfig.h. Then I added INCUDEPATH to my python34 include directory.
#include "mainwindow.h" #include <QApplication> #include <boost/python.hpp> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); using namespace boost::python; PyObject *pName, *pModule, *pDict, *pFunc, *pValue; Py_Initialize(); pName = PyString_FromString(argv[1]); pModule = PyImport_Import(pName); pDict = PyModule_GetDict(pModule); pFunc = PyDict_GetItemString(pDict, argv[2]); if (PyCallable_Check(pFunc)) { PyObject_CallObject(pFunc, NULL); } else { PyErr_Print(); } // Clean up Py_DECREF(pModule); Py_DECREF(pName); Py_Finalize(); return a.exec(); }
My .pro file is:
QT += core gui greaterThan(QT_MAJOR_VERSION, 4): QT += widgets TARGET = TestWidgetApp TEMPLATE = app INCLUDEPATH += C:/boost_1_57_0 INCLUDEPATH += C:/Python34/include SOURCES += main.cpp\ mainwindow.cpp HEADERS += mainwindow.h FORMS += mainwindow.ui OTHER_FILES +=
Then the following errors:
C: \ Python34 \ include \ object.h: 435: error: C2059: syntax error: ';'
C: \ Python34 \ include \ object.h: 435: error: C2238: unexpected token (s) preceding ';'
C: \ Users \ Amol \ Desktop \ TestWidgetApp \ main.cpp: 19: error: C3861: 'PyString_FromString': identifier not found