[C++-sig] How do I wrap virtual methods (solved)

Eric Jardim ericjardim at gmail.com
Fri Aug 5 20:10:40 CEST 2005


Hi all,

I changed the wrapping approach to the wrapper<...>, and with some 
ajustments it worked. 

I will display the code (just the essential part) below:

struct QObject_Wrapper: QObject, wrapper<QObject>
{
// notice that I have overloaded constructors
// so I have to redefine them
 QObject_Wrapper(): 
 QObject()
 {
 }
 
 QObject_Wrapper(QObject* parent):
 QObject(parent)
 {
 }
 
 bool
 event(QEvent* p0)
 {
 if (override event = this->get_override("event"))
 {
// here is the trick. "event" is a Python object
// not a real C++ funcion
 return event( ptr( p0 ) );
 }
 return QObject::event(p0);
 }

 bool
 default_event(QEvent* p0)
 {
 return this->QObject::event(p0);
 }
}; 

void
export_QObject()
{ 
class_<QObject_Wrapper,
boost::shared_ptr<QObject_Wrapper>,
boost::noncopyable>
("QObject")

// constructor with no parent
.def(init<>())

// constructor with QObject parent
.def(init<QObject*>( args("parent") )
[with_custodian_and_ward<1,2>()] )

.def("event", &QObject::event, &QObject_Wrapper::default_event)
;
}

So, it solved the problem. The only thing that I am concerned about, is how 
does it know to convert native "QObject" to the "QObject_Wrapper". Well its 
working well anyway. BTW, Pyste must be updated for that wrapper<...> use.

Thanks everybody. I am enjoying Boost.Python more everyday :)

[Eric Jardim]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20050805/3e0c19a5/attachment.htm>


More information about the Cplusplus-sig mailing list