Quantcast
Channel: Qt DevNet forums: Qt Quick 1283365070**
Viewing all articles
Browse latest Browse all 4972

Why ListModel can contain only Loader instances but not Item

$
0
0
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?

Viewing all articles
Browse latest Browse all 4972