1

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
  • Can you try passing a simple Python object, like a string? Does it call your function or still claim it doesn't match? It sort of looks like the system wants PyObject without the pointer * which is not good. Commented Jun 1, 2017 at 13:13

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.