[PyQt] sip: how to make a python instance owned by C++ without using a parent

Mathias.Born at gmx.de Mathias.Born at gmx.de
Mon Aug 13 22:07:07 BST 2012


Hi,

I have a C++ class "Project", which I expose to Python
via sip.
In Python, I sub-class:

class Derived(Project):
      ...

In addition, there is a Python factory function which creates an
instance of "Derived" and returns it:

def f():
    return Derived(...)

I call this function from withing C++, and feed the return
value into "sipConvertToType", in order to create a wrapper
of type "Project", so I can call methods of the returned
"Derived" instance from within C++, using the wrapper.

Since the "Derived" instance is created by Python, it is
owned by Python.
Is there a way to transfer ownership to C++ without using any
additional objects? I want the Python part to stick to the
C++ wrapper until the latter is destroyed, at which point
the Python part should also be automatically disposed, without
any need to micro-manage the Python part myself.

In the sip sources, I can see a flag "SIP_CPP_HAS_REF" that
might just achieve that, but there appears to be no official
way to use it.

Best Regards,
Mathias Born




More information about the PyQt mailing list