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. Some explanation on destruction
QtWS25 Last Chance

Some explanation on destruction

Scheduled Pinned Locked Moved QML and Qt Quick
1 Posts 1 Posters 572 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.
  • D Offline
    D Offline
    DimanNe
    wrote on last edited by
    #1

    I want to dynamically create and destroy Item described in myComponent (actually, in real world, I want to do this with many items, and I need to manage them, that is why I use ListModel in the example below)

    So here is minimal example, in which all works: as expected
    @
    import QtQuick 2.2

    Item {
    id: root
    property ListModel itemsModel: ListModel { }

    Component {
        id: myComponent
        Item {
            Component.onCompleted: console.log("Component.onCompleted")
            Component.onDestruction: console.log("Component.onDestruction")
        }
    }
    
    Component {
        id: loaderComponent
        Loader { id: loader; sourceComponent: myComponent; }
    }
    Component.onCompleted: {
        var loader = loaderComponent.createObject(root)
    
        // v1 ========================= WORKS =========================
        itemsModel.append({tab: loader})
    
        var temp = itemsModel.get(0).tab
        temp.sourceComponent = undefined
        itemsModel.clear()
    
    
    
        // v2 ========================= DOES NOT WORK =========================
    

    // itemsModel.append(loader)

    // var temp = itemsModel.get(0)
    // temp.sourceComponent = undefined
    // itemsModel.clear()
    }
    }
    @

    It gives following output:
    @
    Component.onCompleted
    Component.onDestruction
    @

    all exactly as expected.

    But, if you comment v1 and uncomment v2, you will not see
    @
    Component.onDestruction
    @

    Output will be only this:
    @
    Component.onCompleted
    @

    So, why in v2 destructor of Item is not called (regardless of temp.sourceComponent = undefined and according to "this":http://qt-project.org/doc/qt-5.0/qtquick/qml-qtquick2-loader.html#active-prop it should be called:

    bq. If the source or sourceComponent changes, any previously instantiated items are destroyed. Setting source to an empty string or setting sourceComponent to undefined destroys the currently loaded object, freeing resources and leaving the Loader empty.

    )?

    1 Reply Last reply
    0

    • Login

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