1

I'm new to Python/SIP and need to create a Python binding for a Qt/C++ library using it.

The class header looks like this:

class MyClass : public QObject {
    Q_OBJECT
    ...
    signals:
        void message(QString msg);
        void data(QVector<quint16> *data);
    ...
};

This is the according SIP specification file:

%Module myclassPy
%Import QtCore/QtCoremod.sip

class MyClass : public QObject
{
%TypeHeaderCode
#include "myclass.h"
%End
    ...
    signals:
        void message(QString msg);
        void data(QVector<quint16> *data);
};

Importing the Python module and connecting the message signal to a Python slot (using PyQt5) work like a charm. However, connecting the data signal doesn't work: TypeError: C++ type 'QVector<quint16>' is not supported as a signal argument type.

So I need to convert the QVector (pointer) argument of the signal to a Python object (probably a list). What is the best way to do this with SIP? Do I need to specify a new signal with a Python type argument?

I already tried using a %MappedType QVector<quint16> definition to convert it to a Python list but that didn't work (same TypeError as above in Python).

Thanks in advance.


UPDATE - I kind of found a solution/workaround:

The data(QVector<quint16> *data) signal got replaced by a signal without argument, e.g. dataAvailable(). Then a public method like QVector<quint16> *getData() is added to transfer the data. The %MappedType QVector<quint16> definition is kept (as I already tried it before) and converts the return type to a Python PyObject *. However, this works with the return value of the getData() method but not with signal arguments.

In Python, just connect the signal dataAvailable() to a slot and call getData() from within this slot.

Therefore the data has to be buffered somewhere within MyClass.

BananaJoh
  • 11
  • 2

0 Answers0