将 python 3.4 嵌入到 C++ Qt 应用程序中? [英] Embedding python 3.4 into C++ Qt Application?

查看:108
本文介绍了将 python 3.4 嵌入到 C++ Qt 应用程序中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个 Qt Quick GUI 应用程序(用于 Windows),它使用 OpenGL 和 C++ 来处理一些计算密集型的东西.我想将 python 代码嵌入到应用程序中,以便做一些在 python 中相对容易的东西.

I'm making an Qt Quick GUI application(for windows), which uses OpenGL and C++ for some computationally intensive stuff. I want to embed python code into the app, for doing some stuff which is comparatively easier in python.

基本上,我只希望 C++ 代码在 python 脚本中调用一个函数,让脚本完成工作,然后将返回的数据存储在变量(字符串或浮点数等)中以供进一步使用.我正在使用 Qt creator,我为 MinGW 编译器获得了 python3 库.我尝试了一些代码,但看起来 python lib 与 Qt creator 不太兼容.为此使用 pyqt 是个好主意吗?执行此操作的最佳和最简单的方法是什么?

Basically, I just want the c++ code to call a function in a python script and let the script do the job, then store the returned data in a variable(string, or float etc.) for further use. I'm using Qt creator, and I got python3 lib for MinGW compiler. I tried some code, but its looks like python lib is not quite compatible with Qt creator. IS using pyqt for this will be a good idea? What will be the best and easiest way to do this ?

这是我试过的基本代码,首先它给了我一个错误,说找不到 pyconfig.h.然后我在我的 python34 包含目录中添加了一个 INCUDEPATH.

This is the basic code I tried, first it gave me an error saying, cannot find pyconfig.h. Then I added an INCUDEPATH to my python34 include directory.

#include "mainwindow.h"
#include <QApplication>
#include <boost/python.hpp>


int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    using namespace boost::python;

    PyObject *pName, *pModule, *pDict, *pFunc, *pValue;

        Py_Initialize();

        pName = PyString_FromString(argv[1]);

        pModule = PyImport_Import(pName);


        pDict = PyModule_GetDict(pModule);


        pFunc = PyDict_GetItemString(pDict, argv[2]);

        if (PyCallable_Check(pFunc))
        {
            PyObject_CallObject(pFunc, NULL);
        } else
        {
            PyErr_Print();
        }

        // Clean up
        Py_DECREF(pModule);
        Py_DECREF(pName);

        Py_Finalize();

    return a.exec();
}

我的 .pro 文件:

My .pro file:

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = TestWidgetApp
TEMPLATE = app

INCLUDEPATH += C:/boost_1_57_0
INCLUDEPATH += C:/Python34/include

SOURCES += main.cpp\
        mainwindow.cpp

HEADERS  += mainwindow.h

FORMS    += mainwindow.ui

OTHER_FILES +=

然后出现以下错误:

C:\Python34\include\object.h:435:错误:C2059:语法错误:';'

C:\Python34\include\object.h:435: error: C2059: syntax error : ';'

C:\Python34\include\object.h:435: 错误: C2238: ';' 前面的意外标记

C:\Python34\include\object.h:435: error: C2238: unexpected token(s) preceding ';'

C:\Users\Amol\Desktop\TestWidgetApp\main.cpp:19:错误:C3861:'PyString_FromString':标识符未找到

C:\Users\Amol\Desktop\TestWidgetApp\main.cpp:19: error: C3861: 'PyString_FromString': identifier not found

推荐答案

这里的问题是 Python 3.4 有一个名为slots"的结构体成员,(文件 object.h,在 PyType_Spec),Qt 将其定义为从您的下方定义,以便您可以这样说:

The problem here is that Python 3.4 has a struct member called "slots", (file object.h, in the typedef for PyType_Spec), which Qt defines out from under you so that you can say things like:

public slots:

在您的代码中.解决方法是添加:

in your code. The solution is to add:

#undef slots

就在包含 Python.h 之前,并在包含任何像 Qt 那样使用插槽"的东西之前重新定义它:

just before you include Python.h, and to redefine it before you include anything that uses "slots" in the way that Qt does:

#undef slots
#include <Python.h>
#define slots

#include "myinclude.h"
#include <QString>

有点小技巧(因为您依赖于 Qt 中 slots 的特定定义),但它应该可以帮助您前进.

A bit of a hack (because you're depending on a particular definition of slots in Qt), but it should get you going.

这篇关于将 python 3.4 嵌入到 C++ Qt 应用程序中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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