Search code examples
qtobjectmodelqmllistmodel

How to pass objects between qml


I want to pass an objects from one qml to other qml. I have a list model.

ListModel {
       id:favouriteapp
           ListElement {
               id: 1
               name:"ABSC"
               title:"5:03pm"
               description:"heloo-1"
               icon1:"images/climate_icon.png"
               icon2:"images/climate_icon.png"
               pinStatus : false
               isLoaded: false

           }
           ListElement {
               id: 2
               name:"GHTT"
               title:"Home at 5:23pm"
               description:"via Gaydon Road-2"
               icon1:"images/navigation_icon.png"
               icon2:"images/navigation_icon.png"
               pinStatus : false
               isLoaded: false

           }
}

Now I want to pass each property of the ListElement via string value to other qml.

How can I pass this entire ListElement object to other qml.


Solution

  • As @folibis said, you need to give a correct id to your object

    ListElement {
        id: obj1
        ...
    }
    

    Here is MyFile.qml to which you can pass the reference to your ListElement object:

    Item{
        ...
        function myFunction(listElementObject){
            //whatever you need to do with your object
        }
    }
    

    And in the same QML file as the ListModel you'll have

    MyFile{
        id: myFileId
    }
    
    Component.onCompleted: myFileId.myFunction(obj1)