Dre*_*led 5 c++ python qt swig
我正在使用 SWIG 为我的 qt 应用程序生成 Python 绑定。我有几个地方使用QList,我想从 SWIG 库中集成像 std::vector 这样的QList(参见http://www.swig.org/Doc1.3/Library.html#Library_nn15)。
这意味着:
为了实现这一点,我使用以下代码:https :
//github.com/osmandapp/OsmAnd-core/blob/master/swig/java/QList.i
后来在我使用 QLists 的课程中,我添加了如下代码:
%import "qlist.i"
%template(listfilter) QList<Interface_Filter*>;
class A {
public:
//.....
QList<Interface_Filter*> get_filters();
};
Run Code Online (Sandbox Code Playgroud)
到目前为止,这有效,但它没有给我与 std::vector 的那种集成。我无法找出 std_vector.i、std_container.i、... 的哪些部分使对象可迭代。
我需要如何扩展 QList 接口文件才能使我的 QList 可迭代?
您所要求的 - 一个 qlist.i swig 文件,QList
在 python 中实现与 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
Run Code Online (Sandbox Code Playgroud)
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;
}
%}
Run Code Online (Sandbox Code Playgroud)
包装以QList
使其及其方法可从 python 访问
这是通过使(部分)类定义可用于 swig 来实现的。这就是你当前qlist.i
所做的。
注意:由于您使用的是指针,因此您可能需要为QList<T*>
typedef 的情况添加“模板专业化” 。否则,will be ,这显然可能会让 swig 感到困惑。(参见swig/Lib/std/std_vector.i)const_reference
const T*
QList
QList<T*>::const_reference
const T*&
python list 和 list 之间的自动转换QList
这一般是通过使用 swig typemaps来实现的。例如,如果您希望函数f(const QList<int>& list)
能够接受 python 列表,则需要指定一个输入类型映射来执行从 python 列表到 的转换QList<int>
:
%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; }
Run Code Online (Sandbox Code Playgroud)
在这里,情况在几个方面更加困难:
QList
:为此,您需要在类型映射中处理这两种情况。T
为 a QList<T>
:T
为普通类型T
。这是通过 swig 函数实现的SWIG_ConvertPtr
。%qlist_conversions(Type)
,您可以使用它将类型映射附加到QList<Type>
特定的Type
.对于其他转换方向(QList
-> python list),您应该首先考虑您想要什么。考虑一个返回QList<int>
. 从 python 调用它,它应该返回一个包装的QList
对象,还是应该自动转换QList
为 python 列表?
QList
以python 序列的形式访问包装,即从 python进行 makelen
和work
为此,您需要使用并实现和方法来扩展qlist.i 文件中的类。[]
QList
%extend { ... }
__len__
__getitem__
如果切片也应该起作用,则需要__getitem__(PySliceObject *slice)__
为 s 提供成员方法和输入以及“类型检查”类型映射PySliceObject
。
如果您希望能够QList
使用[]
python 修改包装中的值,则需要实现__setitem__
.
有关可以实现以实现更好集成的所有有用方法的列表,请参阅有关“内置类型”和“容器的抽象基类”的 python 文档。
注意:如果您使用 swig-builtin
功能,那么您需要使用例如将上述功能另外注册到适当的“插槽”
%feature("python:slot", "sq_length", functype="lenfunc") __len__;
Run Code Online (Sandbox Code Playgroud)使包装的QList
Python可迭代
为此,您需要扩展该类QList
并实现__iter__()
一个返回Python迭代器对象的方法。
Python迭代器对象是一个提供方法__iter__()
和__next__()
(next()
对于较旧的Python)的对象,其中__next__()
返回下一个值并引发Python异常StopIteration
以表示结束。
如前所述,您可以用 python 或 C++ 实现迭代器对象。我展示了一个在 python 中执行此操作的示例。
我希望这有助于您作为调整所需功能的基础。