python - 在 Qt 5 中嵌入 Python3

标签 python c++ qt qmake qt-signals

我想将 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 ';'

enter image description here

这个问题和这个很相似,但是解决方案不起作用

Embedding Python in Qt 5

有人知道怎么解决这个问题吗?或者建议一些干净的解决方法,以便 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/

相关文章:

c++ - 向数组添加维度

python - 将 MS Azure 上麦克风的连续语音识别存储到单独的变量

python - 向量化方程

c++ - 自定义预期失败的完整错误消息 (boost::spirit::x3)

c++ - 术语 "method"是否由 C++ 标准定义?

c++ - 使用 auto 初始化 QFile

c++ - 如何在 C++ 中处理多个冲突类型?

qt - QPlainTextEdit 或 QTextEdit 区域发生变化时,如何编写滚动条跳转到底部/顶部?

python - 单元测试应该如何与外部资源一起工作?什么是正确的做法?

python - numpy 怎么能比我的 Fortran 例程快得多?