我想将 Python 解释器 3.4 嵌入到 Qt 5.2.1 应用程序(64 位)中。 但是我遇到了构建问题,我的意思是当我在 main.cpp 中包含 Python header 时它可以正常编译。
#include <python.h>
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
但是当我把它放在其他地方时(在 Qt 头文件之后)
//
// embedpytest.cpp
//
#include <QLibrary>
#include <python.h>
EmbedPyTest::EmbedPyTest()
{
}
我遇到编译错误:
C:\Python34\include\object.h:435: error: C2059: syntax error : ';'
C:\Python34\include\object.h:435: error: C2238: unexpected token(s) preceding ';'
这个问题和这个很相似,但是解决方案不起作用
有人知道怎么解决这个问题吗?或者建议一些干净的解决方法,以便 python.h 和 Qt5 以后可以幸福地生活在一起吗?
最佳答案
另一种避免与“插槽”相关的冲突的方法是 to locally "park" the offending keyword while Python.h is included, and then reassign it,无需停用关键字信号/插槽/发射(这对于大型 Qt 项目而言可能是不受欢迎的)。 .为此,请将每次出现的 #include "Python.h"
替换为以下 block :
#pragma push_macro("slots")
#undef slots
#include "Python.h"
#pragma pop_macro("slots")
或者,更方便的是,将上面的代码放在它自己的标题中,例如Python_wrapper.h
,并将所有出现的 #include "Python.h"
替换为 #include "Python_wrapper.h"
。
关于python - 在 Qt 5 中嵌入 Python3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23068700/