繁体   English   中英

委托QML中的数组

[英]Delegate an array in QML

我有一个数组,表示为:一,二,三; 在控制台中打印它们时。 我想将它们委托为ListView,但不知道如何?

  1. 使用普通文本元素作为委托。
  2. 保持模型为空。
  3. 加载组件时,在数组上运行循环,然后使用append方法将数据追加到模型中。

尝试让您知道是否卡在某个位置。

所以我做了Amit告诉我的事情,但是我应该更具体一些,因为真正的问题是循环,现在我已经解决了。

我刚刚创建了一个仅具有ID的ListModel。 然后,我通过forEach的一个函数运行该数组,该函数将数组中的每个项目添加到已经创建的ListModel中。 完成!

// 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