【问题标题】:Embedding Python in Qt 5在 Qt 5 中嵌入 Python
【发布时间】:2013-02-25 22:49:48
【问题描述】:

我想将 Python 解释器嵌入到 Qt 5 应用程序中。

我在 Qt 5 中有一个可以工作的应用程序,但是当我放入时

#include <Python.h>

在顶部(Qt 标头下方)编译中断

../sample/python3.3m/object.h:432:23: error: expected member name or ';' after declaration specifiers
PyType_Slot *slots; /* terminated by slot==0. */
~~~~~~~~~~~       ^

当我将 Python 标头放在 Qt 标头上方时,它会中断

In file included from ../Qt5.0.1/5.0.1/clang_64/include/QtGui/QtGui:59:
../Qt5.0.1/5.0.1/clang_64/include/QtGui/qpagedpaintdevice.h:63:57: error: expected '}'
                    A0, A1, A2, A3, A5, A6, A7, A8, A9, B0, B1,
                                                        ^
/usr/include/sys/termios.h:293:12: note: expanded from macro 'B0'
 #define B0      0
                ^
../Qt5.0.1/5.0.1/clang_64/include/QtGui/qpagedpaintdevice.h:62:19: note: to match this '{'
    enum PageSize { A4, B5, Letter, Legal, Executive,
                  ^
1 error generated.

请问,有人知道为什么会这样吗?我可能是因为 Qt 和 Python 定义了一些常用词?我该怎么办?

【问题讨论】:

    标签: python qt compiler-errors python-embedding


    【解决方案1】:

    发生这种情况是因为包含 Python.h 首先间接包含 termios.h,它将 B0 定义为 0,而 qpagedpaintdevice.h 又希望将其用作变量名。在 Qt 包含之后包含 Python.h 与字符串 'slots' 的作用几乎相同。

    我建议以下顺序:

    #include <Python.h>
    #undef B0
    #include <QWhatEver>
    

    【讨论】:

    • 不幸的是,这对我不起作用,因为我正在开发 Qt 插件并且许多文件已经包含 Qt 头文件。这个解决方案最适合我的用例:stackoverflow.com/a/49359288/4085930
    【解决方案2】:

    已接受答案的替代方案:

    由于 Qt 使用 slots 作为保留关键字,因此与 Python API 中 PyType_Spec 结构的 slots 成员的声明存在冲突。

    可以指示 Qt 不使用普通的 moc 关键字,这将消除冲突。这是通过将以下内容添加到您的项目文件中来完成的: CONFIG += no_keywords

    缺点是您需要引用相应的 Qt 宏而不是前面的关键字。

    因此,Qt 端需要以下替换: signals -> Q_SIGNALS slots -> Q_SLOTS emit -> Q_EMIT

    这在 Qt 文档 Using Qt with 3rd Part Signals and Slots 部分的信号和槽中进行了解释。

    PS:在开始一个新项目时,这通常是一个不错的选择,而不是在将 Python 添加到广泛使用 Qt 关键字的现有代码库时。

    【讨论】:

    猜你喜欢
    • 2020-12-11
    • 1970-01-01
    • 2011-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多