Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to read Array values and display in ListView?



  • Hi, I wanted to get and display the array values in the LIstView. The file is a js file.
    I have a file like this.

    data = [ 
    {
    name: 'parent 1'
    child: [{ name: 'child 1'} ,{ imageSource: 'child 2'}]
    },
    {
    name: 'parent 2'
    child: [{ name: 'child 1'} ,{ imageSource: 'child 2'}]
    }]
    

    I wanted to call this js file through model property and then wanted to display it using the delegate property.

    Can any one help me?. Thanks in advance


  • Moderators

    @lakshmanGiri

    ListView {
       property var modelData: [ 
          {
             name: 'parent 1'
             child: [{ name: 'child 1'} ,{ imageSource: 'child 2'}]
          },
          {
             name: 'parent 2'
             child: [{ name: 'child 1'} ,{ imageSource: 'child 2'}]
          }
       ]
    
       model: modelData.length
    
       delegate: Item {
           // access data with "modelData[index]"
       }
    }
    


  • @raven-worx Hi, Thanks for your reply. I tried the method you mentioned, but I couldn't display the data using the delegate property. I tried getting the value by modelData[index] in Text definition within the Item definition. The data was not displayed in the window.

    Am I missing anything?.


  • Moderators

    @lakshmanGiri
    if it's a property of the listview (as in my example) you can try to access the data with:

    ListView.view.modelData[index]

    from within the delegate instead.


Log in to reply