Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Custom Reuse Pool for Delegates
Forum Updated to NodeBB v4.3 + New Features

Custom Reuse Pool for Delegates

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 1 Posters 209 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    ckielwein
    wrote on 13 Aug 2021, 09:03 last edited by
    #1

    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 are TypeError: 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,

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ckielwein
      wrote on 13 Aug 2021, 12:55 last edited by ckielwein
      #2

      I found the issue:
      The parent in the Qt.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-objects

      current 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)

      1 Reply Last reply
      0

      1/2

      13 Aug 2021, 09:03

      • Login

      • Login or register to search.
      1 out of 2
      • First post
        1/2
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved