[C++-sig] pybindgen and PyQt

Gustavo Carneiro gjcarneiro at gmail.com
Thu Aug 21 15:47:40 CEST 2008


2008/8/21 Andreas Eriksson <aeriksson at gmail.com>

> Thanks for the reply Gustavo.
>
> >> does anyone know if pybindgen plays nicely with PyQt (which uses SIP
> >> for wrapping)? Can C++ class wrapped with pybindgen be passed to PyQt?
> >
> > For one thing, I find it hard to believe that PyQt would have any API
> that
> > accepts an arbitrary class.  At most I would believe in PyQt accepting an
> > arbitrary subclass of a well known C++ base class wrapped by PyQt.  So I
> am
> > guessing you mean pybindgen wrapping a subclass of a PyQt-wrapped class.
> > That I'm afraid will not work.
>
> Actually, it is the first scenario that I am after. Let me describe in
> some detail what I am after (slightly simplified):
>
> We have a C++ class that derives from the C++ Qt class QWidget. Lets
> call it OwnWidget.  Several Qt methods expects pointers to subclasses
> of QWidget, such as QLayout::addWidget(QWidget*). Using the PyQt
> bindings I can create a python QLayout object.  Now, what I want to do
> is create a pybindgen wrapped OwnWidget instance and pass it to the
> addWidget method of the PyQt QLayout object.
>
> The following pseudo-code illustrates what I want to do.  I tried it
> using boost.python but without any luck.


I'm sure that boost::python would have had the exact same problem.  There is
no 'standard' for the layout and policy of python object instances wrapping
underlying C++ objects, therefore different wrapping tools cannot subclass
each other's classes.  Moreover, if you wrap in pybindgen both QWidget and
OwnWidget, it may work on its own "world", but then if you pass a OwnWidget
instance into PyQt it will not recognize it as being a pyqt.Widget type.
It's a parallel type hierarchy from the point of view of python.

I'm afraid I really don't have any simple solution for you.  The easiest way
this could work would be via double wrappers.  The same OwnWidget would have
to have an initial wrapper created by pybindgen, then an utility function
that you add to the module would create a pyqt wrapper for the pybindgen
wrapped object, which you can then use in pyqt.  The utility conversion
function needs to be written manually, with knowledge of the SIP API, and
added with Module.add_custom_function_wrapper().  And this only works if you
do not subclass OwnWidget in Python, because pybindgen then assumes it has
the only wrapper for an object; with two wrappers reference counting fails
and you get memory leaks.

It is probably easier just to wrap everything in SIP... :P


>
> C++:
>
>    #include <qt/QWidget.h>
>    class OwnWidget : public QWidget { ... }
>
> python:
>
>    import pyqt
>    import ownwidget    # wrapped using pybindgen
>
>    myWidget = ownwidget.OwnWidget()
>    layout = pyqt.QLayout()
>    layout.addWidget(myWidget)
>
> Regards,
>
> Andreas
> _______________________________________________
> C++-sig mailing list
> C++-sig at python.org <C%2B%2B-sig at python.org>
> http://mail.python.org/mailman/listinfo/c++-sig
>



-- 
Gustavo J. A. M. Carneiro
INESC Porto, Telecommunications and Multimedia Unit
"The universe is always one step beyond logic." -- Frank Herbert
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20080821/764cb15d/attachment.htm>


More information about the Cplusplus-sig mailing list