Kerim
Kerim

Reputation: 199

Using PythonQt wrap function that accepts custom class binded with pybind11

I have a C++ class MyClass that is binded to python using pybind11. Thus MyClass resides in python module named mymodule. That means from python I can do:

import mymodule

my_class = mymodule.MyClass()

Also I have C++ class YourClass:

class YourClass {
public:
  YourCPPObject() {}

  std::string foo(MyClass* myClass){ myClass->getName(); };
};

YourClass must be binded to python using PythonQt in the module named yourmodule. The problem is that the method YourClass::foo(...) expects the parameter MyClass that is binded using pybind11 and it seems that PythonQt doesn't know anythng about MyClass. When I try to bind it using the code:

#include <myclass.h>
#include <string>

#include "PythonQt.h"
#include <QApplication>

class YourClassDecorators : public QObject
{
  Q_OBJECT

public Q_SLOTS:
  YourClass * new_YourClass() { return new YourClass(); }  // constructor
  void delete_YourClass(YourClass* obj) { delete obj; }  // destructor

  // main function that accepts third party class binded with pybind11
  std::string foo(YourClass* yourClass, MyClass* myClass) { return yourClass->foo(myClass); }
};

int main( int argc, char **argv )
{
  QApplication qapp(argc, argv);

  PythonQt::init(PythonQt::IgnoreSiteModule | PythonQt::RedirectStdOut);

  PythonQt::self()->addDecorators(new YourClassDecorators());
  PythonQt::self()->registerCPPClass("MyClass","", "mymodule"); // I don't know maybe I don't need to register MyClass (still doesn't work)
  PythonQt::self()->registerCPPClass("YourClass","", "yourmodule");

  return qapp.exec();
}

I get an error in python:

ValueError: Called foo(MyClass myclass) -> std::string with wrong arguments: (<myclass.MyClass object at 0x7fc52eb92e68>,)

I can't find a way to tell to PythonQt that MyClass is already binded using pybind11.

If somebody has ideas please share.

Upvotes: 1

Views: 121

Answers (0)

Related Questions