[PyQt] QML garbage collects the model

Phil Thompson phil at riverbankcomputing.com
Wed Jul 23 14:05:55 BST 2014

On 22/07/2014 3:02 pm, Nenad Ognjanovic wrote:
> This is a follow up on the previous thread "QML, pyqtSlot and 
> ownership".
> The problem I'm having is that QML garbage collects the model which is
> in use by ListView. ListView initially gets the model by calling
> Python function (pyqtSlot) which instantiates and returns the model
> (the function changes the ownership of the model before returning).
> This works fine until the garbage collector gets triggered, at which
> point QML will collect the model, and ListView will have missing items
> after scrolling.
> I have prepared the example that demonstrates this, which can easily
> be tweaked to use different object ownership transfer methods and
> model data types, as described in the source code.
> The code is attached, and can also be checked out at this gist:
> https://gist.github.com/bgr/1e43b12f088e07465afb
> In its core the code could be 3x shorter, but I've expanded it to
> cover a couple of different approaches. There is a simpler version of
> the code at revision #1 of the gist.
> I have also tried registering DummyModel and DummyObject with
> qmlRegisterType after I've uploaded the example, it also has no
> effect.
> I have no clue whether it's a bug or if I'm doing something totally
> wrong, but one important thing to note is that when
> GUIEntryPoint.get_model is converted to be a pyqtProperty the problem
> doesn't occur.

The fact that the model is collected implies that the ownership has been 
transferred correctly. But I have no idea why QML should think the model 
is no longer needed - smells like a Qt bug.

A C++ version of your example would probably be useful to compare with.


More information about the PyQt mailing list