2013-02-25 78 views
6

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

我在Qt的5工作的应用程序,但是,当我把

#include <Python.h> 

在顶部(以下Qt的报头)的汇编场所与

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

当我把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. 

请问,有没有人知道为什么会发生这种情况?我可能是因为Qt和Python定义了一些常见词汇?我能做些什么呢?

回答

6

发生这种情况是因为包含Python.h第一个间接包含termios.h,它将B0定义为0,这反过来qpagedpaintdevice.h想要用作变量名称。在Qt包含之后加入Python.h与字符串'slots'相反。

我建议按以下顺序:

#include <Python.h> 
#undef B0 
#include <QWhatEver> 
2

到接受的答案的选择:

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

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

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

因此,下面的替代将需要Qt的侧: signals -> Q_SIGNALS slots -> Q_SLOTS emit -> Q_EMIT

这在上信号和槽Qt的文档上的部分Using Qt with 3rd Part Signals and Slots说明。

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