在Qt 5中嵌入Python

Eci*_*ana 7 python qt compiler-errors python-embedding

我想将Python解释器嵌入到Qt 5应用程序中.

我在Qt 5中有一个工作应用程序,但是当我放入时

#include <Python.h>
Run Code Online (Sandbox Code Playgroud)

在顶部(Qt标题下方)编译中断

../sample/python3.3m/object.h:432:23: error: expected member name or ';' after declaration specifiers
PyType_Slot *slots; /* terminated by slot==0. */
~~~~~~~~~~~       ^
Run Code Online (Sandbox Code Playgroud)

当我将Python标题放在Qt标题之上时,它会破坏

In file included from ../Qt5.0.1/5.0.1/clang_64/include/QtGui/QtGui:59:
../Qt5.0.1/5.0.1/clang_64/include/QtGui/qpagedpaintdevice.h:63:57: error: expected '}'
                    A0, A1, A2, A3, A5, A6, A7, A8, A9, B0, B1,
                                                        ^
/usr/include/sys/termios.h:293:12: note: expanded from macro 'B0'
 #define B0      0
                ^
../Qt5.0.1/5.0.1/clang_64/include/QtGui/qpagedpaintdevice.h:62:19: note: to match this '{'
    enum PageSize { A4, B5, Letter, Legal, Executive,
                  ^
1 error generated.
Run Code Online (Sandbox Code Playgroud)

拜托,有谁知道为什么会这样?我可能是因为Qt和Python定义了一些常用词?我能做些什么呢?

axx*_*xel 6

这是因为包括Python.h首先间接包括termios.h,它将B0定义为0,而qpagedpaintdevice.h又想要用作变量名.在Qt包含之后包含Python.h与字符串'slots'相反的方式完全相同.

我建议以下顺序:

#include <Python.h>
#undef B0
#include <QWhatEver>
Run Code Online (Sandbox Code Playgroud)


CJC*_*ink 5

已接受答案的替代方案:

由于 Qt 使用 theslots作为保留关键字,因此与Python API 中结构slots成员的声明存在冲突PyType_Spec

可以指示 Qt 不使用普通的 moc 关键字,这将消除冲突。这是通过将以下内容添加到您的项目文件来完成的: CONFIG += no_keywords

缺点是您将需要引用相应的 Qt 宏而不是前面的关键字。

因此,Qt 端将需要以下替换: signals -> Q_SIGNALS slots -> Q_SLOTS emit -> Q_EMIT

这在使用 Qt 与第三部分信号和插槽部分的信号和插槽的 Qt 文档中进行了解释。

PS:当开始一个新项目时,这通常是一个不错的选择,而不是将 Python 添加到广泛使用 Qt 关键字的现有代码库时。