Embedding python 3.4 into C++ Qt Application?

杀马特。学长 韩版系。学妹 提交于 2021-01-27 04:37:10

问题


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


回答1:


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.




回答2:


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.




回答3:


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*).




回答4:


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.



来源:https://stackoverflow.com/questions/27045323/embedding-python-3-4-into-c-qt-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!