python – swig:如何使QList可迭代,如std :: vector
我正在使用SWIG为我的qt应用程序生成
Python绑定.我有几个地方,我使用
QLists,我想整合那些QLists,如SWIG图书馆的std :: vector(见
http://www.swig.org/Doc1.3/Library.html#Library_nn15).
这意味着: > QList对象应该可以从python迭代(=它们必须是可迭代的python对象) 为此,我使用以下代码: %import "qlist.i" %template(listfilter) QList<Interface_Filter*>; class A { public: //..... QList<Interface_Filter*> get_filters(); }; 这个工作到目前为止,但它没有给我与std :: vector的集成. 解决方法
您要求的是 – 一个qlist.i swig文件,它在python中实现与QList相同的集成级别,就像std_vector.i对std :: vector一样 – 是一项非常重要的任务.
我提供了一个非常基本的扩展qlist.i文件(和qlisttest.i来向您展示如何使用它),并将尝试解释所需的步骤. qlist.i: %{ #include <QList> %} %pythoncode %{ class QListIterator: def __init__(self,qlist): self.index = 0 self.qlist = qlist def __iter__(self): return self def next(self): if self.index >= self.qlist.size(): raise StopIteration; ret = self.qlist.get(self.index) self.index += 1 return ret __next__ = next %} template<class T> class QList { public: class iterator; typedef size_t size_type; typedef T value_type; typedef const value_type& const_reference; QList(); size_type size() const; void reserve(size_type n); %rename(isEmpty) empty; bool empty() const; void clear(); %rename(add) push_back; void push_back(const value_type& x); %extend { const_reference get(int i) throw (std::out_of_range) { int size = int(self->size()); if (i>=0 && i<size) return (*self)[i]; else throw std::out_of_range("QList index out of range"); } void set(int i,const value_type& val) throw (std::out_of_range) { int size = int(self->size()); if (i>=0 && i<size) (*self)[i] = val; else throw std::out_of_range("QList index out of range"); } int __len__() { return self->size(); } const_reference __getitem__(int i) throw (std::out_of_range) { int size = int(self->size()); if (i>=0 && i<size) return (*self)[i]; else throw std::out_of_range("QList index out of range"); } %pythoncode %{ def __iter__(self): return QListIterator(self) %} } }; %define %qlist_conversions(Type...) %typemap(in) const QList< Type > & (bool free_qlist) { free_qlist = false; if ((SWIG_ConvertPtr($input,(void **) &$1,$1_descriptor,0)) == -1) { if (!PyList_Check($input)) { PyErr_Format(PyExc_TypeError,"QList or python list required."); SWIG_fail; } Py_ssize_t len = PyList_Size($input); QList< Type > * qlist = new QList< Type >(); free_qlist = true; qlist->reserve(len); for (Py_ssize_t index = 0; index < len; ++index) { PyObject *item = PyList_GetItem($input,index); Type* c_item; if ((SWIG_ConvertPtr(item,(void **) &c_item,$descriptor(Type *),0)) == -1) { delete qlist; free_qlist = false; PyErr_Format(PyExc_TypeError,"List element of wrong type encountered."); SWIG_fail; } qlist->append(*c_item); } $1 = qlist; } } %typemap(freearg) const QList< Type > & { if (free_qlist$argnum and $1) delete $1; } %enddef qlisttest.i: %module qlist; %include "qlist.i" %inline %{ class Foo { public: int foo; }; %} %template(QList_Foo) QList<Foo>; %qlist_conversions(Foo); %inline %{ int sumList(const QList<Foo> & list) { int sum = 0; for (int i = 0; i < list.size(); ++i) { sum += list[i].foo; } return sum; } %} >包装QList以使其及其方法可从python访问 %typemap(in) const QList<int> & { PyObject * py_list = $input; [check if py_list is really a python list of integers] QList<int>* qlist = new QList<int>(); [copy the data from the py_list to the qlist] $1 = qlist; } %typemap(freearg) const QList<int> & { if ($1) delete $1; } 在这方面,情况在几个方面更加困难: >您希望能够传递python列表或包装的QList:为此,您需要在typemap中处理这两种情况. 对于其他转换方向(QList – > python列表),您应该首先考虑您想要的内容.考虑一个返回QList< int>的C函数.从python调用它,它应该返回一个包装的QList对象,还是应该自动将QList转换为python列表? 如果切片也应该有效,则需要为PySliceObjects提供__getitem __(PySliceObject * slice)__成员方法和输入以及“类型检查”类型图. 如果您希望能够使用python中的[]修改包装QList中的值,则需要实现__setitem__. 有关可以实现以实现更好集成的所有有用方法的列表,请参阅有关“内置类型”和“容器的抽象基类”的python文档. 注意:如果您使用swig -builtin功能,则需要使用例如以下方法将上述功能注册到相应的“插槽”. %feature("python:slot","sq_length",functype="lenfunc") __len__; >使包装的QList可以从python中迭代 python迭代器对象是一个对象,它提供方法__iter __()和__next __()(next()用于较旧的python),其中__next __()返回下一个值并引发python异常StopIteration以指示结束. 如前所述,您可以在python或C中实现迭代器对象.我展示了在python中执行此操作的示例. 我希望这有助于您调整所需的功能. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |