Passing numpy object to pythonqt wrapper

171 views Asked by At

I have a c++ class myClass and I'm trying to have a creator based on a numpy array.

Here is the wrapper defining the new_ python creator taking PyObject as argument:

class myClassPyWrapper : public QObject {
    Q_OBJECT

public slots:
/*... many other creators here ...*/
    myClass* new_myClass(PyObject* my_py_obj){
        if (PyArray_Check(my_py_obj)) {
            //do something
        }
        return nullptr;
    }
}

But the creator doesn't get called and from within Python when I type:

a=np.array(np.zeros(100)).reshape(10,10)
b=myClass(a)

I get:

py> a=np.array(np.zeros(100)).reshape(10,10)
py> b=myClass(a)
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ValueError: Could not find matching overload for given arguments:
(array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]]),)
 The following slots are available:
myClass() -> myClass
myClass(PyObject my_py_obj) -> myClass
myClass(myClass) -> myClass
myClass(int, int) -> myClass
myClass(int, int, double val) -> myClass
myClass(int, int, double val, QString name) -> myClass
myClass(QVector<double>, QPair<int,int>) -> myClass

I tried by replacing Pyobject* with PyArrayObject* but I get the same error.

Every other creator works.

What am I missing?

1

There are 1 answers

0
bibi On

I post here the answer I got directly from PythonQt developper, in case someone get the same problem:

The short answer is that the method was returning a nullptr and the Qt metasystem was detecting (I don't know how) and was not calling the method.

By replacing return nullptr with a generic contructor (e.g. return new myClass()) solves the problem.