[PyQt] Keeping the wrapper alive when it would go out of scope

Phil Thompson phil at riverbankcomputing.com
Tue Dec 4 09:06:20 GMT 2018


On 4 Dec 2018, at 1:42 am, Nyall Dawson <nyall.dawson at gmail.com> wrote:
> 
> Hi list,
> 
> If I have a method in a class with a /Transfer/ed argument:
> 
>   void addTask( MyClass* task /Transfer/ );
> 
> and I attempt to call it from a code which looks like this:
> 
>   def scoped_function():
>        t = MyClass()
>        t.some_python_property = 555
>        m.addTask( t )
> 
>   scoped_function()
> 
> Then the Python wrapper for t (but not the transferred cpp object) is
> deleted when t goes out of scope at the end of the function call, as
> expected.
> 
> Is there anyway to indicate to sip that the lifetime of the wrapper
> should be bound to the cpp object, and only deleted when the cpp
> object is? What I'm trying to achieve is that the wrapper, including
> the python-only "some_python_property", is not cleaned up when
> scoped_function() exits, and is instead only cleaned up when the
> MyClass cpp base class object is deleted.

That is what /Transfer/ does. You need to provide a complete test to demonstrate the problem.

Phil


More information about the PyQt mailing list