qt调用python脚本变量传输_QT 调用Python文件并传输参数

(1) python文件如下

#!/usr/bin/env python

# -*- coding: utf-8 -*-

def hello():

print("hello world!")

def greatFunc():

print("hello world,greatFunc!")

def Add(a, b , para):

print("111232");

return a+b;

(2) qt 调用代码如下

void MainWindow::on_pushButton_py_3_clicked()

{

//进行初始化

Py_Initialize();

//如果初始化失败,返回

if(!Py_IsInitialized())

{

qDebug()<

}

PyRun_SimpleString("import sys");

PyRun_SimpleString("sys.path.append('./')");

//加载模块,模块名称为myModule,就是myModule.py文件

PyObject *pModule = PyImport_ImportModule("myModule");

//如果加载失败,则返回

if(!pModule)

{

qDebug()<

return;

}

else

{

PyObject* pRet=PyObject_CallMethod(pModule,"Add","iis",8,9,"woshi");

if (!pRet)

{

printf("不能找到 pRet");

return ;

}

int info;

PyArg_Parse(pRet, "i", &info);

qDebug()<

}

Py_Finalize();

}

(3) 完美输出结果17。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值