将python 3.4嵌入到c++ Qt应用程序中?

时间:2022-07-24 18:37:41

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.

我正在做一个Qt Quick GUI应用程序(适用于windows),它使用OpenGL和c++进行一些计算密集型的工作。我想将python代码嵌入到应用程序中,因为在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 ?

基本上,我只是希望c++代码在python脚本中调用一个函数,并让脚本完成工作,然后将返回的数据存储在一个变量中(字符串,或者float等)以供进一步使用。我使用的是Qt创建者,我得到了MinGW编译器的python3 lib。我尝试了一些代码,但是它看起来像python lib,与Qt创建者不太兼容。使用pyqt是一个好主意吗?做这件事最好最简单的方法是什么?

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.

编辑:这是我尝试过的基本代码,首先它给了我一个错误,说不能找到pyconfig.h。然后我添加了一个INCUDEPATH到我的python34包含目录。

#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:

我.pro文件:

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 \ \包括对象。h:435:错误:C2059:语法错误:';

C:\Python34\include\object.h:435: error: C2238: unexpected token(s) preceding ';'

C:\ Python34 \ \包括对象。h:435:错误:C2238:意外令牌(s)

C:\Users\Amol\Desktop\TestWidgetApp\main.cpp:19: error: C3861: 'PyString_FromString': identifier not found

C:\Users\Amol\Desktop\ TestWidgetApp \主要。cpp:19:错误:C3861:“PyString_FromString”:未找到标识符。

4 个解决方案

#1


2  

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:

这里的问题是,Python 3.4有一个名为“槽”的结构成员(file对象)。h,在PyType_Spec的typedef中,Qt从你的下面定义,这样你就可以这样说:

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:

在包括Python之前。h,在你包含任何用Qt的方式使用“槽”的东西之前重新定义它:

#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.

有点黑(因为你依赖于Qt中插槽的特定定义),但它应该能让你前进。

#2


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.

我已经从您的示例中删除了所有Qt代码,然后尝试编译它(Qt与您的编译错误无关)。它为我编译。区别在于,我使用了Python 2.7中的include文件。

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.

所以我在文件夹中搜索了字符串PyString_FromString: C:\Python33\包括(我注意到您使用的是python 3.4而不是3.3,但是我怀疑这是一个3)。x的事情)和C:\ Python27 \包括。

Results:

结果:

Python 3.3

Python 3.3

将python 3.4嵌入到c++ Qt应用程序中?

Python 2.7

Python 2.7

将python 3.4嵌入到c++ Qt应用程序中?

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

因此,显然,Python 3.4不支持您的BoostPython版本。

#3


0  

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

Python3没有PyString_FromString函数。Python3 str类型内部是具有复杂结构的unicode对象。

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

使用PyUnicode_FromString或PyUnicode_FromStringAndSize来构造来自UTF-8编码的C字符串(char*)的str对象。

#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.

真正的问题是Scott Deerwester在他的回答中所描述的。

#1


2  

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:

这里的问题是,Python 3.4有一个名为“槽”的结构成员(file对象)。h,在PyType_Spec的typedef中,Qt从你的下面定义,这样你就可以这样说:

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:

在包括Python之前。h,在你包含任何用Qt的方式使用“槽”的东西之前重新定义它:

#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.

有点黑(因为你依赖于Qt中插槽的特定定义),但它应该能让你前进。

#2


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.

我已经从您的示例中删除了所有Qt代码,然后尝试编译它(Qt与您的编译错误无关)。它为我编译。区别在于,我使用了Python 2.7中的include文件。

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.

所以我在文件夹中搜索了字符串PyString_FromString: C:\Python33\包括(我注意到您使用的是python 3.4而不是3.3,但是我怀疑这是一个3)。x的事情)和C:\ Python27 \包括。

Results:

结果:

Python 3.3

Python 3.3

将python 3.4嵌入到c++ Qt应用程序中?

Python 2.7

Python 2.7

将python 3.4嵌入到c++ Qt应用程序中?

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

因此,显然,Python 3.4不支持您的BoostPython版本。

#3


0  

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

Python3没有PyString_FromString函数。Python3 str类型内部是具有复杂结构的unicode对象。

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

使用PyUnicode_FromString或PyUnicode_FromStringAndSize来构造来自UTF-8编码的C字符串(char*)的str对象。

#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.

真正的问题是Scott Deerwester在他的回答中所描述的。