在boostpython模块内部从pyside导入类?

2024-06-28 19:14:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我想使用pySead定义基本的Qt类和C++和Python之间的映射,但是在Python:Python:Python中使用独立的Python代码和嵌入的Python来这样做。在

首先,模块定义和返回QPointF的类:

QPointF  X::getY() { 
  return QPointF(); 
}    

BOOST_PYTHON_MODULE(myBoostPythonModule)
{
// is there some magic init/register commands to put here?
boost::python::api::object module     = import("__main__");
boost::python::api::object name_space = module.attr("__dict__");
boost::python::exec("from PySide.QtCore import *",name_space,name_space);
boost::python::exec("import sys,os\nprint(sys.modules)",name_space,name_space);

class_<X, boost::noncopyable>(
            "X", init<const char*>())
        .def("getY",&X::getY)
        ;
}

现在,应用程序的嵌入式python代码,最后一行是失败的,我想知道如何解决:

^{pr2}$

这里发生了什么,我可以创建一个QPointF,但是名称不知怎么地没有绑定在python和c++之间?我是否在模块中丢失了一些导入来告诉它从PySide导入?在


Tags: 模块代码nameimportapi定义objectinit
2条回答

根据前面的回复和我发现的其他信息,这里有一个通用的例程,允许像PySide.QtGui.QColor参数这样的东西传递到一个需要QColor&输入参数的PySide.QtGui.QColor包装的c++方法中:

template<class QtGuiClass,int SBK_BOGAN_IDX>
struct QtGui_from_python {
QtGui_from_python() {
  qDebug()<<" registering type: " << typeid(this).name() << " in " << __FUNCTION__;
  boost::python::converter::registry::push_back(
    &convertible,
    &construct,
    boost::python::type_id<QtGuiClass>());
}
static void* convertible(PyObject* obj_ptr) {
    if(!PyObject_TypeCheck(obj_ptr,
            (PyTypeObject*)XSbkPySide_QtGuiTypes[SBK_BOGAN_IDX]))
    {    qDebug()<<"Failed type check!?"; }
    return obj_ptr;
}
static void construct( PyObject* obj_ptr, 
   bp::converter::rvalue_from_python_stage1_data* data)
{
    void* storage = (
    (boost::python::converter::rvalue_from_python_storage<QtGuiClass>*)
    data)->storage.bytes;

    SbkObject* result = reinterpret_cast<SbkObject*>(obj_ptr);
    auto _ptr = (QtGuiClass*) (Shiboken::Object::cppPointer(
                                       result,Py_TYPE(result)));
    new (storage) QtGuiClass(*_ptr);
    qDebug() << "__alloc'd " << typeid(*_ptr).name() <<" at "<<_ptr;
    data->convertible = storage;
}
};

并且,从启动函数调用上述函数:

^{pr2}$

PySide使用Shiboken提供其Qt绑定。Shiboken生成pythoncapi绑定,这些绑定支持自己的类型转换系统。这些转换的知识存在于Shiboken生成的绑定中,而不是Python类型系统中。因此,pySead知道如何将^ {< CD1> }对象转换为C/C++/Python;Python的类型系统不转换。在

当对象通过用Boost.Python,那么Boost.Python将检查其注册表以查找适当的类型转换器。这些转换器提供Boost.Python关于如何将C/C++/Python转换为通过类型公开的知识Boost.Python. 因此,什么时候Boost.Python试图将^ {< CD1> } C++类型返回到Python,因为转换未注册,它抛出异常。Boost.Python. 在

下面是带注释的代码:

import myBoostPythonModule
from PySide.QtCore import *
...
x=myBoostPythonModule.X('foo') # Boost.Python knows how to convert C++ X
                               # to Python X.  Python's type system does not.

w=QPointF()                    # Shiboken knows how to convert C++ QPointF to
                               # Python QPointF.  Python's type system does not.
print(w)                       # Shiboken knows how to represent C++ QPointF as
                               # a string.

y=x.getY()                     # Boost.Python knows how to invoke X::getY(),
                               # but only Shiboken knows how to convert C++
                               # QPointF to Python QPointF.  Thus, the TypeError
                               # exception is raised.

有可能实现Boost.Python的converters表示另一个实现。展开Shiboken type converter example,下面是一个完整的示例Boost.Python的转换器使用Shiboken的老式转换器实现。我本来会使用新的Shiboken类型转换器API,但我不清楚它是基于文档的什么。在

^{pr2}$

以及它的用法:

>>> import example
>>> x = example.make_complex(4, 2)
complex_converter_to_python::convert()
Shiboken::Converter<Complex>::toPython()
>>> example.print_complex(x)
complex_converter_from_python::convertible()
Shiboken::Converter<Complex>::isConvertible()
complex_converter_from_python::construct()
Shiboken::Converter<Complex>::toCpp()
In print_complex: 4, 2

另一种方法,虽然不是最优雅的方法,但是可以从Boost.Python使用boost::python::object类型,并通过Python语句与对象接口。类似于:

boost::python::object X::getY()
{ 
  return boost::python::exec("QPointF()", ...); 
}

上面的代码将让Python实例化一个QPointFPython对象,该对象将委托给Shiboken的类型系统。当X::getY()返回一个泛型对象时,Boost.Python当对象句柄从C++过渡到Python时,将不会尝试执行类型转换。在

相关问题 更多 >