【问题标题】:Delegate an array in QML在 QML 中委托一个数组
【发布时间】:2013-07-11 22:33:39
【问题描述】:

我有一个数组,它的形式如下:一、二、三;在控制台中打印它们时。 我想将这些委托为 ListView 但不知道如何?

【问题讨论】:

标签: javascript arrays delegates qml


【解决方案1】:
  1. 使用普通文本元素作为委托。
  2. 保持模型为空。
  3. 在加载组件时,对数组运行循环并使用append 方法将数据附加到模型中。

如果您在某个时候遇到困难,请尝试并告知。

【讨论】:

    【解决方案2】:

    所以我按照 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-11
      • 2020-12-04
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多