Custom Reuse Pool for Delegates
-
I'm currently trying to implement a reuse pool for delegates of a GridView.
(See https://forum.qt.io/topic/129246/gridview-reuse-pool)
My delegates are stateless, so keeping and properly recreating their state is not an issue.Currently I'm trying to store them in an Array and restore them later.
Adding delegates to the pool seems to work fine.
Restoring from the pool works sometimes, but after a time, the elements of the array areTypeError: Type error
.Log:
... qml: push to pool QQuickText(0x5613036c9770) qml: pool after push: [QQuickText(0x5613036c9770)] qml: pool before pop: [TypeError: Type error] ...
What is going wrong here?
I suspect the GC is removing the items in the array, how can I check that?relevant code:
GridView { id: poolView property var pool: [] model: 1200 delegate: Item{ id: holder property var current: null Component.onCompleted: { if (poolView.pool.length === 0) { console.debug("create new", poolView.pool) holder.current = Qt.createQmlObject('import QtQuick 2.0; Text {color: "red"; text: index}', holder, "dynamicSnippet1"); } else { console.debug("pool before pop: ", poolView.pool) holder.current = poolView.pool.pop(); holder.current.parent = holder } holder.current.visible = true } Component.onDestruction: { console.debug("push to pool", holder.current) if (holder.current){ holder.current.visible = false poolView.pool.push(holder.current) } console.debug("pool after push:", poolView.pool) } } }
A full example here: https://github.com/CasparKielwein/qml_item_pool/blob/main/main.qml
Thanks,
-
I found the issue:
The parent in theQt.createQmlObject()
call needs to be above the delegate.
Lifetime of the created object is bound to the parent given in the call.
(Somewhat surprising in a garbage collected language if you ask me)
Source: https://doc.qt.io/qt-5/qtqml-javascript-dynamicobjectcreation.html#maintaining-dynamically-created-objectscurrent code:
holder.current = Qt.createQmlObject('import QtQuick 2.0; Text {color: "red"; text: index}', poolView, "dynamicSnippet1");
My current issue now is that the model data does not seem to be updated properly.
(e.g. the index is not updated when an Item is reused from the pool)