Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embedding python 3.4 into C++ Qt Application?

I'm making an Qt Quick GUI application(for windows), which uses OpenGL and C++ for some computationally intensive stuff. I want to embed python code into the app, for doing some stuff which is comparatively easier in python.

Basically, I just want the c++ code to call a function in a python script and let the script do the job, then store the returned data in a variable(string, or float etc.) for further use. I'm using Qt creator, and I got python3 lib for MinGW compiler. I tried some code, but its looks like python lib is not quite compatible with Qt creator. IS using pyqt for this will be a good idea? What will be the best and easiest way to do this ?

EDIT: This is the basic code I tried, first it gave me an error saying, cannot find pyconfig.h. Then I added an 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:

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

like image 856
Amol Borkar Avatar asked Nov 20 '14 17:11

Amol Borkar


1 Answers

The problem here is that Python 3.4 has a struct member called "slots", (file object.h, in the typedef for PyType_Spec), which Qt defines out from under you so that you can say things like:

public slots:

in your code. The solution is to add:

#undef slots

just before you include Python.h, and to redefine it before you include anything that uses "slots" in the way that Qt does:

#undef slots
#include <Python.h>
#define slots

#include "myinclude.h"
#include <QString>

A bit of a hack (because you're depending on a particular definition of slots in Qt), but it should get you going.

like image 71
Scott Deerwester Avatar answered Oct 14 '22 10:10

Scott Deerwester