调用 python 函数
call a python function
我需要从 qt c++ (qmake) 调用 python 并在它们之间共享数据。所以我写了这段代码但是错误是合乎逻辑的,我的意思是没有错误但是它意外停止了。
我也用 python2.7 试过了,但还是一样的错误。我想将 a 传递给 someFunction 函数并将 return 传递回 main.cpp
代码:
someFunction.py:(我把它放在项目的“其他文件”部分)
def someFunction(a):
return a
main.cpp:
#include <Python.h>
#undef B0
#include "mainwindow.h"
#include <QApplication>
#include <QDebug>
int main(int argc, char *argv[])
{
Py_Initialize();
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append(\".\")");
PyObject* myModuleString = PyUnicode_FromString((char*)"arbName");
PyObject* myModule = PyImport_Import(myModuleString);
PyObject* myFunction = PyObject_GetAttrString(myModule,(char*)"someFunction");
PyObject* args = Py_BuildValue("(d)", 2.0);
PyObject *myResult = PyObject_CallObject(myFunction, args);
qDebug() << "Hello World";
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
qmake:
QT += core gui
INCLUDEPATH = /usr/include/python3.8
LIBS += -lpython3.8
CLANG_INSTALL_DIR += usr/lib/clang/10/lib/linux
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
CONFIG += no_keywords
SOURCES += \
main.cpp \
mainwindow.cpp
HEADERS += \
mainwindow.h
FORMS += \
mainwindow.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
DISTFILES += \
someFunction.py
您有 2 个问题:
您使用 someFunction
但模块名为 arbName
。
“.”它指示应用程序执行的目录,如果您使用 QtCreator,它是构建目录而不是源代码文件夹。另一方面,最好避免使用相对路径,而是构建绝对路径。为此,您必须使用 QCoreApplication::applicationDirPath()
。除了复制文件并自动执行之外,您还可以使用 this answer.
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
CONFIG += no_keywords
INCLUDEPATH = /usr/include/python3.8
LIBS += -lpython3.8
CONFIG += file_copies
COPIES += python
python.files = someFunction.py
python.path = $$OUT_PWD
SOURCES += \
main.cpp \
mainwindow.cpp
HEADERS += \
mainwindow.h
FORMS += \
mainwindow.ui
DISTFILES += \
someFunction.py
main.cpp
#include "mainwindow.h"
#include <Python.h>
#include <QDebug>
#include <QApplication>
int main(int argc, char *argv[])
{
Py_Initialize();
QString sys_path_append_command = QString(R"(sys.path.append("%1"))").arg(QCoreApplication::applicationDirPath());
PyRun_SimpleString("import sys");
PyRun_SimpleString(sys_path_append_command.toStdString().c_str());
PyObject* myModuleString = PyUnicode_FromString((char*)"someFunction");
PyObject* myModule = PyImport_Import(myModuleString);
PyObject* myFunction = PyObject_GetAttrString(myModule,(char*)"someFunction");
PyObject* args = Py_BuildValue("(f)", 2.0);
PyObject *myResult = PyObject_CallObject(myFunction, args);
qDebug() << "Hello World" << PyFloat_AsDouble(myResult);
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
我需要从 qt c++ (qmake) 调用 python 并在它们之间共享数据。所以我写了这段代码但是错误是合乎逻辑的,我的意思是没有错误但是它意外停止了。
我也用 python2.7 试过了,但还是一样的错误。我想将 a 传递给 someFunction 函数并将 return 传递回 main.cpp
代码:
someFunction.py:(我把它放在项目的“其他文件”部分)
def someFunction(a):
return a
main.cpp:
#include <Python.h>
#undef B0
#include "mainwindow.h"
#include <QApplication>
#include <QDebug>
int main(int argc, char *argv[])
{
Py_Initialize();
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append(\".\")");
PyObject* myModuleString = PyUnicode_FromString((char*)"arbName");
PyObject* myModule = PyImport_Import(myModuleString);
PyObject* myFunction = PyObject_GetAttrString(myModule,(char*)"someFunction");
PyObject* args = Py_BuildValue("(d)", 2.0);
PyObject *myResult = PyObject_CallObject(myFunction, args);
qDebug() << "Hello World";
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
qmake:
QT += core gui
INCLUDEPATH = /usr/include/python3.8
LIBS += -lpython3.8
CLANG_INSTALL_DIR += usr/lib/clang/10/lib/linux
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
CONFIG += no_keywords
SOURCES += \
main.cpp \
mainwindow.cpp
HEADERS += \
mainwindow.h
FORMS += \
mainwindow.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
DISTFILES += \
someFunction.py
您有 2 个问题:
您使用
someFunction
但模块名为arbName
。“.”它指示应用程序执行的目录,如果您使用 QtCreator,它是构建目录而不是源代码文件夹。另一方面,最好避免使用相对路径,而是构建绝对路径。为此,您必须使用
QCoreApplication::applicationDirPath()
。除了复制文件并自动执行之外,您还可以使用 this answer.
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
CONFIG += no_keywords
INCLUDEPATH = /usr/include/python3.8
LIBS += -lpython3.8
CONFIG += file_copies
COPIES += python
python.files = someFunction.py
python.path = $$OUT_PWD
SOURCES += \
main.cpp \
mainwindow.cpp
HEADERS += \
mainwindow.h
FORMS += \
mainwindow.ui
DISTFILES += \
someFunction.py
main.cpp
#include "mainwindow.h"
#include <Python.h>
#include <QDebug>
#include <QApplication>
int main(int argc, char *argv[])
{
Py_Initialize();
QString sys_path_append_command = QString(R"(sys.path.append("%1"))").arg(QCoreApplication::applicationDirPath());
PyRun_SimpleString("import sys");
PyRun_SimpleString(sys_path_append_command.toStdString().c_str());
PyObject* myModuleString = PyUnicode_FromString((char*)"someFunction");
PyObject* myModule = PyImport_Import(myModuleString);
PyObject* myFunction = PyObject_GetAttrString(myModule,(char*)"someFunction");
PyObject* args = Py_BuildValue("(f)", 2.0);
PyObject *myResult = PyObject_CallObject(myFunction, args);
qDebug() << "Hello World" << PyFloat_AsDouble(myResult);
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}