0

我有一个 C++ 类MyClass,它使用pybind11. 因此MyClass驻留在名为mymodule. 这意味着从 python 我可以做到:

import mymodule

my_class = mymodule.MyClass()

我也有 C++ 类YourClass

class YourClass {
public:
  YourCPPObject() {}

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

YourClass必须PythonQt在名为yourmodule. 问题是该方法YourClass::foo(...)需要使用绑定的参数MyClasspybind11并且似乎PythonQtMyClass. 当我尝试使用代码绑定它时:

#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();
}

我在 python 中遇到错误:

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

我找不到一种方法来告诉PythonQtMyClass已经使用pybind11.

如果有人有想法,请分享。

4

0 回答 0