在Qt 5中嵌入Python3 [英] Embedding Python3 in Qt 5

查看:66
本文介绍了在Qt 5中嵌入Python3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to embed Python interpreter 3.4 into a Qt 5.2.1 application (64-bit). However I'm having build issues, I mean when I include Python header in the main.cpp it compiles fine.

#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标头之后)

but when I put it anywhere else (after Qt headers)

//
// 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 ';'

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

It's very similar problem to this one, but the solution is not working

在Qt 5中嵌入Python

任何人都知道如何解决此问题?或建议一些干净的解决方法,以便python.h和Qt5以后可以快乐地生活在一起?

Anyone knows how to solve this issue ? or suggest some clean workaround so that python.h and Qt5 can live together happily ever after ?

推荐答案

令人反感的行是这样的:

The offending line is this:

PyType_Slot *slots; /* terminated by slot==0. */

问题在于,在此行中,"slots"是Qt中默认的关键字.为了在其他项目中使用该变量名,您将需要在项目文件中使用该变量名:

The problem is that with this line, "slots" is a keyword by default in Qt. In order to use that variable name in other projects, you will need to use this in your project file:

CONFIG += no_keywords

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

For details, see the documentation:

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

Using Qt with 3rd Party Signals and Slots

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

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

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

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.

这篇关于在Qt 5中嵌入Python3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆