在QT应用程序中嵌入Python3.5脚本

时间:2019-05-09 06:47:40

标签: python c++ python-3.x qt

我正在尝试将包含类的python脚本(python 3.5)嵌入到我的QT c ++应用程序中。在尝试GUI之前,我尝试了一个简单的控制台应用程序。问题是当我选择“ QT控制台应用程序”作为新项目类型时,代码崩溃了,但是如果我选择了“普通C ++应用程序”(非QT项目选项),程序将按预期工作!

Python脚本:

import numpy as np
import dlib

# some other imports that also imports other modules

class myclass:

    def __init__(self, path1, path2, path3, use_Test=True):

        self.p1 = path1
        self.p2 = path2
        self.p3 = path3

        self.test = use_Test

    # some other functions

    def add(self, x, y):
        return x+y

这是我在普通C ++应用程序中尝试的代码:

.pro文件中的第一个:

TEMPLATE = app
CONFIG += console c++11
CONFIG -= app_bundle
CONFIG -= qt

SOURCES += main.cpp

INCLUDEPATH += /usr/include/python3.5
LIBS += -L/usr/lib/python3.5/config-3.5m-x86_64-linux-gnu -lpython3.5

C ++代码:

#include <iostream>
#include <Python.h>


using namespace std;

int main()
{
    setenv("PYTHONPATH", ".", 1);

    Py_Initialize();

    PyObject* module = PyImport_ImportModule("myscript");
    assert(module != NULL);

    PyObject* dict = PyModule_GetDict(module);
    assert(module != NULL);
    Py_DECREF(module);

    PyObject* python_class = PyDict_GetItemString(dict, "myclass");
    assert(python_class != NULL);
    Py_DECREF(dict);

    PyObject* engine;
    if (PyCallable_Check(python_class))
    {
        PyObject *args = Py_BuildValue("(sss)", "s1", "s2", "s3");
        PyObject *keywords = PyDict_New();
        PyDict_SetItemString(keywords, "tester", Py_False);

        engine = PyObject_Call(python_class, args, keywords);
        Py_DECREF(python_class);
        Py_DECREF(args);
        Py_DECREF(keywords);
    }
    else
    {
        std::cout << "Cannot instantiate the Python class" << std::endl;
        Py_DECREF(python_class);
        return 1;
    }

    PyObject *value = PyObject_CallMethod(engine, "add", "(ii)", 2, 4);

    int result = PyLong_AsLong(value);

    cout<<"Result = "<<result<<endl;

    cout << "Hello World!" << endl;

    Py_Finalize();

    return 0;
}

此代码正常工作,我从add函数获得了所需的结果。但是转到QT控制台应用程序,代码将变为:

PRO:

QT -= gui

CONFIG += c++11 console
CONFIG -= app_bundle

CONFIG += no_keywords    #Solve Python SLOT ISSUE

# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += main.cpp


INCLUDEPATH += /usr/include/python3.5
#INCLUDEPATH += /usr/local/lib/python3.5/dist-packages/numpy/core/include/numpy
LIBS += -L/usr/lib/python3.5/config-3.5m-x86_64-linux-gnu -lpython3.5

C ++

#include <QCoreApplication>

#include <Python.h>


#include <QDir>
#include <iostream>
using namespace std;

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    setenv("PYTHONPATH", ".", 1);

    Py_Initialize();

    PyObject* module = PyImport_ImportModule("myscript");
    assert(module != NULL);

    PyObject* dict = PyModule_GetDict(module);
    assert(module != NULL);
    Py_DECREF(module);

    PyObject* python_class = PyDict_GetItemString(dict, "myclass");
    assert(python_class != NULL);
    Py_DECREF(dict);

    PyObject* engine;
    if (PyCallable_Check(python_class))
    {
        PyObject *args = Py_BuildValue("(sss)", "s1", "s2", "s3");
        PyObject *keywords = PyDict_New();
        PyDict_SetItemString(keywords, "tester", Py_False);

        engine = PyObject_Call(python_class, args, keywords);
        Py_DECREF(python_class);
        Py_DECREF(args);
        Py_DECREF(keywords);
    }
    else
    {
        std::cout << "Cannot instantiate the Python class" << std::endl;
        Py_DECREF(python_class);
        return 1;
    }

    PyObject *value = PyObject_CallMethod(engine, "add", "(ii)", 2, 4);

    int result = PyLong_AsLong(value);

    cout<<"Result = "<<result<<endl;

    cout << "Hello World!" << endl;

    Py_Finalize();

    return a.exec();

}

但是每当我尝试运行代码时,它就会崩溃(崩溃发生在PyObject *模块= PyImport_ImportModule(“ myscript”);行)

我在SO中寻找解决方案,并提出了一个答案,建议在Py_Initialize()之后添加以下内容;

QString qs = QDir::currentPath();
std::wstring ws = qs.toStdWString();
PySys_SetPath(ws.data());

这没有使代码崩溃,但现在我所有的python导入都出错了(即:ImportError:没有名为“ numpy”的模块)

我在QT APP中缺少什么? QT和Python之间有冲突吗?

0 个答案:

没有答案