在Qt 5中嵌入Python3

kru*_*sty 8 c++ python qt qmake qt-signals

我想将Python解释器3.4嵌入到Qt 5.2.1应用程序(64位)中.但是我有构建问题,我的意思是当我在main.cpp中包含Python头时它编译得很好.

#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();
}
Run Code Online (Sandbox Code Playgroud)

但是当我把它放在其他地方时(在Qt标题之后)

//
// embedpytest.cpp
//
#include <QLibrary>
#include <python.h>


EmbedPyTest::EmbedPyTest()
{
}
Run Code Online (Sandbox Code Playgroud)

我得到编译错误:

C:\Python34\include\object.h:435: error: C2059: syntax error : ';'
C:\Python34\include\object.h:435: error: C2238: unexpected token(s) preceding ';'
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

这与此问题非常相似,但解决方案无效

在Qt 5中嵌入Python

谁知道如何解决这个问题?或建议一些干净的解决方法,以便python.h和Qt5能够幸福地生活在一起吗?

and*_*sdr 10

另一种避免与'slot'冲突的方法,无需停用关键字signals/slots/emit(这对于大型Qt项目来说可能是不合需要的),是在包含Python.h时在本地"停放"有问题的关键字,然后重新分配它.要实现此目的,请使用#include "Python.h"以下块替换每次出现的事件:

#pragma push_macro("slots")
#undef slots
#include "Python.h"
#pragma pop_macro("slots")
Run Code Online (Sandbox Code Playgroud)

或者,更方便,把上面的代码在它自己的头,例如Python_wrapper.h,和替换所有出现#include "Python.h"#include "Python_wrapper.h".


lpa*_*app 9

违规行是这样的:

PyType_Slot *slots; /* terminated by slot==0. */
Run Code Online (Sandbox Code Playgroud)

问题是,对于这一行,"slot"在Qt中默认是一个关键字.要在其他项目中使用该变量名,您需要在项目文件中使用它:

CONFIG += no_keywords
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请参阅文档:

使用Qt与第三方信号和插槽

可以将Qt与第三方信号/插槽机制一起使用.您甚至可以在同一个项目中使用这两种机制.只需将以下行添加到qmake项目(.pro)文件即可.

CONFIG += no_keywords
Run Code Online (Sandbox Code Playgroud)

它告诉Qt不要定义moc关键字信号,插槽和发射,因为这些名称将由第三方库使用,例如Boost.然后继续使用带有no_keywords标志的Qt信号和插槽,只需将源中Qt moc关键字的所有使用替换为相应的Qt宏Q_SIGNALS(或Q_SIGNAL),Q_SLOTS(或Q_SLOT)和Q_EMIT.