|
3Qter豆
以下是Qt中的代码
pro中
- QT += widgets
- SOURCES += \
- main.cpp
- INCLUDEPATH += -I F:\Python35\include\
- LIBS += F:\Python35\libs\ -lpython35
- DISTFILES += \
- B.py
- CONFIG += no_keywords
复制代码 main函数:
- #include <QCoreApplication>
- #include "Python.h"
- #include <QDebug>
- int main(int argc, char *argv[])
- {
- QCoreApplication a(argc, argv);
- Py_Initialize();
- if (!Py_IsInitialized())
- {
- return -1;
- }
- PyObject* pModule = PyImport_ImportModule("B");
- if (!pModule) {
- qDebug() << "Cant open python file!";
- return -1;
- }
- PyObject* pFunhello= PyObject_GetAttrString(pModule,"hello");
- if(!pFunhello) {
- qDebug() << "Get function hello failed";
- return -1;
- }
- PyObject_CallFunction(pFunhello,NULL);
- Py_Finalize();
- return a.exec();
- }
复制代码 Python代码:
- def hello():
- print("hello")
复制代码 错误:
|
最佳答案
查看完整内容
试试我的工程,运行之后,把B.py拷贝到release目录下
|