[PyQt] pyqtSlot and garbage collector

marcos hipe marcos91_3 at hotmail.com
Thu Apr 5 17:20:39 BST 2018


Hello list!
I'm a little new here and with qt and pyqt, so i don't know what i'm doing
wrong

I searched the problem and found this link [0] with the same problem (from
2014) and with no solution

The problem is that i need to return a list of QObject in a Slot written in
Python to a QML, and when the list arrives to the QML it is a list of
undefined elements. The guess is that the garbage collector delete the
objects from the list before they arrive to the QML.

In [1] I make some test with a minimum example. In the main1 example, the
Slot return a list with two elements of a type that inherit from QObject
and have a simple property of a QString type, and in the output the
console.log print the list with two null objects.
In the main2 examples I change the return type of the Slot to a
QQmlListProperty, and the result is similar, but now the elements are
undefined.
In the main3 example I change the Slot in main2 to a pyqtProperty, and
change the call in the qml to suit that change and the list arrives
perfectly.
In the main4 i take the main1 example, but i convert the list in a instance
variable, so the garbage collector will not remove the elements in the
list, and the list arrives perfectly to the QML.

Is there some good way to solve this? I am doing something wrong?

Thank to read me!!

[0] http://python.6.x6.nabble.com/QML-garbage-collects-the-
model-td5064672.html
[1] https://gist.github.com/marcoshipe/81473609c0927bea0a17828de29cc2a9
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20180405/c1364f32/attachment.html>


More information about the PyQt mailing list