qt传递二维数组,列表给python

test2.py
# -*- coding: utf-8 -*-
from __future__ import print_function
#...
#if __name__ = '__main__':
def func1(a):
#...
	return a
def test2(UP, LISAN, DOWNEST):
	#UP =  {1.1,2,3};
	#LISAN= {{1,2,3,4,5},{1,2,3,4,5,6,7}}
	#DOWNEST= -1.0e+100
	#...
	# Mubiaos= ...
	# Fangans= ...
	return min(Mubiaos), Fangans, Mubiaos
# Mubiao = [1.2 3.5 5.6 4.6 7.8]
# min(Mubiaos) = 1.2
# Fangans = [[1.3, 3.5, 6.7],[1.2, 4.5],[4.5, 7.8, 3.2, 5.7]]

hello.pro
INCLUDEPATH += -I C:\Python27\include

INCLUDEPATH += -I  C:\Python27\Lib\site-packages\numpy\core\include
INCLUDEPATH += -I  C:\Python27\Lib\site-packages\numpy
LIBS += -L C:\Python27\libs -lpython27
DEPENDPATH += -l  C:\Python27\include

系统环境:

PYTHONPATH="C:\python27"

hello.cpp
#include <Python.h>
#include <iostream>
#include <QString>
#include <math>
#include <numpy/arrayobject.h>
#include <random/randomkit.h>
//#include <synchapi.h>  sleep()
using namespace std;

void test1();
void test2();
void hello::hello(QWidget *parent):
	QDialog(parent),
	ui(new Ui::hello)
{
	ui->setupUi(this);
	Py_SetPythonHome("c:\\Python27");
	Py_Initialize();
	
}
void hello::~hello()
{
	Py_Finalize();
	delete ui;
}
void hello::on_btnTest_clicked()
{
	test1();
	test2();
}

void test2()
{
	if (!Py_isInitialize()) return;
	import_array()

	PyRun_simpleString("import sys");
	QString setSysPath = QString("sys.path.append('%1')").arg(QCoreApplication::applicationDirPath());
	PyRun_SimpleString(setSysPath.toStdString().c_str());

	PyObject* pModule = PyImport_ImportModule("test2");
	if (!pModule)
	{
		  qDebug("Can not open python file!");  return;               
	}

	PyObject * pFunc = PyObject_GetAttrString(pModule, "test2Function");
	if (!pFunc)
	{
	   qDebug("Get Function failed!"); return;
	}
	
	PyObject* listUP = PyList_New(3);
	double arrUP[] = {1.1,2,3};
	for (int i = 0; i < 3; i++)
	{
		PyList_SetItem(listUP, i, Py_BuildValue("d", arrUP[i]));
	}
	
	PyObject* listLISAN1 = PyList_New(5);
	for (int i = 0; i < 5; i++)
	{
		PyList_SetItem(listLISAN1, i, Py_BuildValue("i", i+1);
	}
	PyObject* listLISAN2 = PyList_New(7);
	for (int i = 0; i < 7; i++)
	{
		PyList_SetItem(listLISAN2, i, Py_BuildValue("i", i+1);
	}
	PyObject* listLISAN = PyList_New(0);
	PyList_Append(listLISAN, listLISAN1);
	PyList_Append(listLISAN, listLISAN2);
	
	PyObject* DOWNEST = PyFloat_FromDouble(-1.0e+100);
	
	PyObject* args = PyTuple_New(3);
	PyTuple_SetItem(args, 0, listUP);
	PyTuple_SetItem(args, 1, listLISAN);
	PyTuple_SetItem(args, 2, DOWNEST);
	
	PyObject* res = PyEval_CallObject(pFunc, args);
	
	PyObject* poMinMubiaos = PyTuple_GetItem(res, 0);
	PyObject* poFanggans = PyTuple_GetItem(res, 1);
	PyObject* poMubiaos = PyTuple_GetItem(res, 2);
	
	{
		qDebug("MinMubiaos = %f\r\n", PyFloat_AsDouble(poMinMubiaos));
	}
	{
		//cout << "poFanggans type: " << poFanggans->ob_type->tp_name << endl << flush; // type: list
		cout << "Fangans=[\r\n" << flush;
		for (int i = 0; i < PyList_Size(poFanggans); i++)
		{
			PyObject* childList = PyList_GetItem(poFanggans, i);
			cout << "[" << flush;
			for (int j = 0; j < PyList_Size(childList): j++)
			{
				if (j != 0) cout << ", " << flush;
				cout << PyFloat_AsDouble(PyList_GetItem(childList, j)) << flush;
			}
			cout << "], \r\n" << flush;
		}
		cout << "] \r\n" << flush;
	}
	{
		cout << "poMinMubiaos size = " << PyArray_Size(poMinMubiaos) << endl;
		PyArrayObject* pyArr = reinterpret_cast<PyArrayObject*>(poMinMubiaos);
		int nd = pyArr->nd; // 维度
		if (nd == 1)
		{
			int row = pyArr->dimensions[0];
			assert(row == PyArray_Size(poMinMubiaos));
			cout << "poMinMubiaos=[" << flush;
			for (int i = 0; i < row; i++)
			{
				if (i != 0) cout << " " flush;
				cout << *((double*)(pyArr->data + i * pyArr->strides[0])) << flush;
			}
			cout << "]\r\n" << flush;
		}
	}
}

void test1()
{
	Py_SetPythonHome("c:\\Python27");
	import_array()

	PyRun_simpleString("import sys");
	QString setSysPath = QString("sys.path.append('%1')").arg(QCoreApplication::applicationDirPath());
	PyRun_SimpleString(setSysPath.toStdString().c_str());

	PyObject* pModule = PyImport_ImportModule("test1");
	if (!pModule)
	{
		  qDebug("Can not open python file!");  return;               
	}

	PyObject * pFunc = PyObject_GetAttrString(pModule, "test1Function");
	if (!pFunc)
	{
	   qDebug("Get Function failed!"); return;
	}

// data: [[1 2 3], [1 2 3]]
	const int SIZE1 = 2;
	const int SIZE2 = 3;
	const int ND = 2;
	npy_intp dims[ND] = {SIZE1, SIZE2};
	double (*c_arr)[SIZE2] = new double[SIZE1][SIZE2];
	for (int i = 0; i < SIZE1; i++)
		for (int j = 0; j < SIZE2; j++)
			c_arr[i][j] = i*SIZE1+j;

	pyArrayObject *pyArr;
	pyArr = reinterpret_cast<PyArrayObject*>(
				PyArray_SimpleNewFromData(ND, dims, NPY_LONGDOUBLE,
					reinterpret_cast<void*>(c_arr)));

	PyObject *args = PyTuple_New(1);
	PyTuple_SetItem(args, 0, reinterpret_cast<PyObject*>(pyArr));
	PyEval_CallObject(pFunc, args);
	delete[] c_arr;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值