Qt调用Python正确用法

1:首先你要先安装python;这个不缀述:

 

2:装好 之后,文件会有

pro文件加上:INCLUDEPATH += ./include

然后在pro文件右键添加静态库:把libs里面的都添加进去

 

3:错误总结:

参考:https://blog.csdn.net/itas109/article/details/78733584这里面的1-3写的都挺好的。

 

4:最终你会遇到python里面写了类然后又写了__init__函数 的。再用之前的调用就不好使 了

所以参考:https://blog.csdn.net/hnlylyb/article/details/89498651?utm_medium=distribute.wap_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-9.nonecase&depth_1-utm_source=distribute.wap_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-9.nonecase

这是我的py文件

# -*- coding: utf-8 -*-
 
class GcodeToTrack:

    def __init__(self,gcode_file,qwer):
        #初始化类
        #读取gcode文档
        print(gcode_file)
        print(qwer)
        print("hello world")

    def hello(self,adad):
        print("hello")
        print(adad);

   
        

这是我的代码:

#include <QCoreApplication>
#include <Python.h>
#include <QDebug>

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

    Py_Initialize();
    if( !Py_IsInitialized() ){
        return 0;
    }
    //执行单句Python语句,用于给出调用模块的路径,否则将无法找到相应的调用模块
    PyRun_SimpleString("import sys");
    QString setSysPath = QString("sys.path.append('%1')").arg(QCoreApplication::applicationDirPath());
    PyRun_SimpleString(setSysPath.toStdString().c_str());


    //导入hello.py模块
    PyObject* pModule = PyImport_ImportModule("TransferKRL");
    if (!pModule) {
        QString infoData = "Can not open python file!";
        qDebug() << infoData;
        return 0;
    }


    PyObject *pyClass = PyObject_GetAttrString(pModule, "GcodeToTrack");
    PyObject *pConstruct = PyInstanceMethod_New(pyClass);

    //创建参数 参考https://blog.csdn.net/yangjf91/article/details/93062961?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param
    //调用的正确方法(带有__init__函数的):https://blog.csdn.net/hnlylyb/article/details/89498651?utm_medium=distribute.wap_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-9.nonecase&depth_1-utm_source=distribute.wap_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-9.nonecase
    PyObject* pParams = PyTuple_New(2);
    PyTuple_SetItem(pParams, 0, Py_BuildValue("s", "ArcWall.ngc"));
    PyTuple_SetItem(pParams, 1, Py_BuildValue("s", "trackFile03.csv"));
    PyObject* pIns = PyObject_CallObject(pConstruct,pParams);

    PyObject* pFunhello= PyObject_GetAttrString(pIns,"hello");
    PyObject* pParams1 = PyTuple_New(1);
    PyTuple_SetItem(pParams1, 0, Py_BuildValue("s","ArcWall.ngc"));
    PyObject *FuncTwoBack = PyObject_CallObject(pFunhello, pParams1);//获取返回值

    int res = 0;
    PyArg_Parse(FuncTwoBack,"i",&res);//转换返回类型
    qDebug()<<"res:"<<res;

    Py_Finalize();
    return a.exec();
}

 

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

搁浅的渔

创作不易,多多支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值