作者:虚坏叔叔
早餐店不会开到晚上,想吃的人早就来了!😄
完成Qt项目创建并引入python

一、创建QT项目
创建一个项目PyPlayer:

选择QWidget:

为了后面调试的方便,将子系统设置为控制台:

二、项目中引入python,并完成python初始化
从之前的cpp_call_py项目,将python的环境拷贝到Pyplayer中:

添加./include,引入python的头文件目录:

初始化python的环境
#include <Python.h>
#include "PyPlayer.h"
PyPlayer::PyPlayer(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
Py_SetPythonHome(L"./");
Py_Initialize();
}
三、载入pyqt模块
添加python文件pyqt.py

pyqt.py的内容如下:
print("Python PyPlayer")
#主函数 在子线程中调用,线程是c++创建
def main():
print("Python main")
在c++端载入模块:
#include <Python.h>
#include "PyPlayer.h"
static PyObject *pModule = 0;
PyPlayer::PyPlayer(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
Py_SetPythonHome(L"./");
Py_Initialize();
// 载入模块
pModule = PyImport_ImportModule("pyqt");
if (!pModule)
{
printf("PyImport import error");
PyErr_Print();
return;
}
// 开启线程 调用python的 main函数
}
可以看到载入模块成功了:

四、总结
- 本文使用
QT创建了项目并引入python,并且这个项目加载python模块。










