简体   繁体   English

委托QML中的数组

[英]Delegate an array in QML

I have an array, which is presented like: one,two,three; 我有一个数组,表示为:一,二,三; when printing them in console. 在控制台中打印它们时。 I want to delegate these as a ListView but don't know how? 我想将它们委托为ListView,但不知道如何?

  1. Use a normal text element as the delegate. 使用普通文本元素作为委托。
  2. Keep the model empty. 保持模型为空。
  3. On component loading, run a loop over the array and append the data into model using the append method. 加载组件时,在数组上运行循环,然后使用append方法将数据追加到模型中。

Try and let know if you get stuck at some point. 尝试让您知道是否卡在某个位置。

So I kind of did what Amit told me to, but I should have been more specific since the real issue I had was the loop, which I've now resolved. 所以我做了Amit告诉我的事情,但是我应该更具体一些,因为真正的问题是循环,现在我已经解决了。

I just created a ListModel with just an ID. 我刚刚创建了一个仅具有ID的ListModel。 Then I ran the array trough a function from forEach, which added each of the items in the array to the already created ListModel. 然后,我通过forEach的一个函数运行该数组,该函数将数组中的每个项目添加到已经创建的ListModel中。 Done! 完成!

// create the listmodel to use
ListModel {
    id: playlistModel
}

// function that adds each playlist in the listmodel to show it in the app
function addtoPlaylistModel(element,index,array) {
    console.debug("Debug: Playlist #" + index + " = " + element);
    playlistModel.append({"id": index, "name": element});
}

    // get playlists in an array
    var playlist = playlist1,playlist2,playlist3
    console.debug("Debug: Playlists: "+playlist) //debug
    playlist.forEach(addtoPlaylistModel) // send each item on playlist array to the model to show it

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM