7

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

4
  • 1
    Can you show the code you tried and explain why it didn't work? Commented Nov 20, 2014 at 17:30
  • 1
    In my opinion it would be much easier to build the app in PyQt + PyOpenGL and if necessary do the computation in a C++ module. PyQt and PyOpenGL are wrappers around C++ code, so it does not slow down. And if your computations use common routines (e.g. linear approximation, matrix multiplication etc.), that have also Python libraries written in C++, you may skip the whole C++ part. Commented Nov 20, 2014 at 17:39
  • 3
    RTFM Embedding Python in Another Application Commented Nov 20, 2014 at 18:03
  • OK, I edited and added the code I tried, can you guys please remove the hold now? Also where should I place the py file which contains the multiply function? (Code based on the following article: codeproject.com/Articles/11805/Embedding-Python-in-C-C-Part-I ) Commented Nov 23, 2014 at 9:02

4 Answers 4

4

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

So I did a little search for the string PyString_FromString in the folders: C:\Python33\includes (I noted you use python 3.4 and not 3.3 but I suspect this is a 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 BoostPython version.

Comments

0

Python3 has no PyString_FromString function. Python3 str type internally is unicode objects with complex structure.

Use PyUnicode_FromString or PyUnicode_FromStringAndSize for constructing str object from UTF-8 encoded C string (char*).

2 Comments

It is still giving me these two errors:C:\Python34\include\object.h:435: error: C2059: syntax error : ';' C:\Python34\include\object.h:435: error: C2238: unexpected token(s) preceding ';'
Are you sure that you use right python version in compilation toolset? Like bjam target-os=windows/python=3.4? Or at least define PY_MAJOR_VERSION=3 and PY_MINOR_VERSION=4.
-1

Move your

#include "boost/python.hpp" 

...to be before your other includes and it will resolve your problem.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.