[PyQt] QML, pyqtSlot and ownership

Nenad Ognjanovic bgr.gyk at gmail.com
Mon Jul 14 16:49:04 BST 2014

Phil Thompson <phil <at> riverbankcomputing.com> writes:

> How would it work in C++?
> In other words, is the PyQt behaviour correct and in C++ you rely on
> memory leaks (ie. the object existing without there being a reference
> to it)?
> Or is there a PyQt ownership bug?

As far as the first scenario goes (returning QObjects from slots), the
object does get garbage collected by the QML engine so it's not exactly a
memory leak. I also cannot claim that it's a PyQt bug, but from Qt
framework standpoint the documentation does say that the C++ object
ownership should be taken over by QML if the parent wasn't assigned to
the instantiated object:

> When data is transferred from C++ to QML, the ownership of the data
> always remains with C++. The exception to this rule is when a QObject
> is returned from an explicit C++ method call: in this case, the QML
> engine assumes ownership of the object, unless the ownership of the
> object has explicitly been set to remain with C++ by invoking
> QQmlEngine::setObjectOwnership() with QQmlEngine::CppOwnership
> specified.
> Additionally, the QML engine respects the normal QObject parent
> ownership semantics of Qt C++ objects, and will not ever take ownership
> of a QObject instance which already has a parent.


The problem when python comes into play is that object never reaches QML
so it doesn't get a chance to get taken over. I'd expect PyQt to
intervene here and keep the object alive, so that it's congruent with the
default Qt behavior.

For the second one I'd expect it to also work, since the Dummy is being
explicitly instantiated in QML. I figured it'd be best to provide sample
code for it also - I've posted it as a gist here:
https://gist.github.com/bgr/5072f0bb3d1c5623b6c1 as well as attached to
this message. If you comment out the sip.transferto call, it'll segfault
as soon as the list is scrolled. With the call in place, everything works
but the app doesn't exit on close.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: pyqt_qml_ownership_reversed.py
Type: text/x-python
Size: 1411 bytes
Desc: not available
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20140714/68e8208d/attachment.py>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: zzz.qml
Type: text/x-qml
Size: 538 bytes
Desc: not available
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20140714/68e8208d/attachment.bin>

More information about the PyQt mailing list