Embedding python 3.4 in a C ++ Qt application?

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

+6
source share
4 answers

The problem is that Python 3.4 has a structure element called “slots” (object.h file, in typedef for PyType_Spec ), which Qt defines from under you so you can say things like:

 public slots: 

in your code. The solution is to add:

 #undef slots 

before you enable Python.h and override it before you include everything that uses “slots” in the way Qt does it:

 #undef slots #include <Python.h> #define slots #include "myinclude.h" #include <QString> 

A bit of a hack (because you depend on the specific definition of slots in Qt), but it should catch you.

+2
source

I removed all the Qt code from your example and then tried to compile it (Qt has nothing to do with your compilation error). And it compiles for me. The difference was that I used include files from Python 2.7 .

So, I searched a little for the PyString_FromString line in the folders: C:\Python33\includes (I noticed that you are using python 3.4, not 3.3, but I suspect that this is 3.x thing) and C:\Python27\includes .

Results:

Python 3.3

Searching string PyString_FromString in Python33's include folder

Python 2.7

Searching string PyString_FromString in Python27's include folder

So, apparently, Python 3.4 is not supported by your version of BoostPython.

0
source

Python3 does not have a PyString_FromString function. The Python3 str type internally is Unicode objects with a complex structure.

Use PyUnicode_FromString or PyUnicode_FromStringAndSize to construct a str object from UTF-8 encoded C string ( char* ).

0
source

Move

 #include "boost/python.hpp" 

... be before your other one turns on and he solves your problem.

The actual issue is what Scott Deerwester described in his answer.

-1
source

Source: https://habr.com/ru/post/978471/


All Articles