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. Why ListModel can contain only Loader instances but not Item
Forum Updated to NodeBB v4.3 + New Features

Why ListModel can contain only Loader instances but not Item

Scheduled Pinned Locked Moved QML and Qt Quick
1 Posts 1 Posters 644 Views 1 Watching
  • 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

    Let's consider such example
    @
    import QtQuick 2.2

    Item {
    id: root
    property ListModel itemsModel: ListModel { }
    property Component delegateComponent: Item {
    objectName: "item with function"
    function f() { console.log("ffffffffffffffffffffffffffffffffffff") }
    }

    Component {
        id: loaderComponent
        Loader {
            id: loader
            objectName: "loader"
            sourceComponent: delegateComponent
        }
    }
    
    Component.onCompleted: {
        // ======================= v1 DOES NOT WORK =======================
    

    // var item = delegateComponent.createObject(root)
    // itemsModel.append(item)
    // console.log("objectName ", itemsModel.get(0).objectName)
    // itemsModel.get(0).f()

        // ======================= v2 WORKS =======================
        var item = loaderComponent.createObject(root)
        console.log("Adding ", item)
        itemsModel.append(item)
        console.log("objectName ", itemsModel.get(0).objectName, itemsModel.get(0).item.objectName)
        itemsModel.get(0).item.f()
    }
    

    }
    @

    There is all good, and its works as intended, here is output:
    @
    Adding QQuickLoader(0x2422840, "loader")
    objectName loader item with function
    ffffffffffffffffffffffffffffffffffff
    @

    Via Component's function createObject we create instance of Loader (var item = loaderComponent.createObject(root)), then add this instance of Loader in ListModel (itemsModel.append(item)), and, finally, call function f().

    But, if you comment v2 and uncomment v1, you will get this output:
    @
    objectName item with function
    file:///home/.../MainWindow.qml:26: TypeError: Property 'f' of object ModelObject(0x1c2e680) is not a function
    @

    In v1 I try to add created by Component Item (var item = delegateComponent.createObject(root)) directly (not wrapped in Loader) in ListModel but it seems something goes wrong, and I can not call f() function.

    So, why ListModel can store Loader instances, but can not store Item instances?

    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