在Qt 5中嵌入Python3

前端之家收集整理的这篇文章主要介绍了在Qt 5中嵌入Python3前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想将 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();
}

但是当我把它放在其他地方时(在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 ';'

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

Embedding Python in Qt 5

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

解决方法

违规行是这样的:
PyType_Slot *slots; /* terminated by slot==0. */

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

CONFIG += no_keywords

有关详细信息,请参阅documentation

Using Qt with 3rd Party Signals and Slots

It is possible to use Qt with a 3rd party signal/slot mechanism. You can even use both mechanisms in the same project. Just add the following line to your qmake project (.pro) file.

CONFIG += no_keywords

It tells Qt not to define the moc keywords signals,slots,and emit,because these names will be used by a 3rd party library,e.g. Boost. Then to continue using Qt signals and slots with the no_keywords flag,simply replace all uses of the Qt moc keywords in your sources with the corresponding Qt macros Q_SIGNALS (or Q_SIGNAL),Q_SLOTS (or Q_SLOT),and Q_EMIT.

原文链接:https://www.f2er.com/python/185558.html

猜你在找的Python相关文章