1

我正在编写我的第一个 Qt Quick 应用程序,但在做其他环境中简单的事情时遇到了很多麻烦。我想显示一个文件夹中的 n 个随机图像。为此,我使用了 FolderListModel,但问题是我没有看到任何直接访问文件列表的方法。所以这是我的hackish方法:

  • 使用 FolderListModel 读取文件
  • 使用 Text 组件作为委托
  • 文本组件的 onTextChanged 获取文件名(这部分有效)并将其添加到一些 ListModel
  • 随机化 ListModel,然后用它来显示文件

我有很多问题和疑问,但首先,这样做的明智方法是什么(请这样做,以便列表部分不必用 C++ 编写)?

现在有 2 个问题 - 我不知道如何从 Text 组件访问 ListModel;而且我不知道如何使 ListModel 公开/可从另一个显示图像的组件访问。

下面是代码:

import QtQuick 1.0
import Qt.labs.folderlistmodel 1.0

ListView {
    width: 200; height: 300


    FolderListModel {
        folder: "file:///C:/somefolder"
        id: folderModel
        nameFilters: ["*.jpg"]
    }

    Component {
        id: fileDelegate
        Text { id: intext
               text: fileName
               //the next line fails, Can't find variable: anotherModel
               onTextChanged: anotherModel.append([{name: intext.text}]
             )
        }
    }

    model: folderModel
    delegate: fileDelegate


    ListModel {
          id: anotherModel
      }
}
4

1 回答 1

2

你的代码几乎对我有用。我收到错误“QML ListModel:追加:值不是对象”。这是因为您附加了数组。如果删除行中的括号

anotherModel.append([{name: intext.text}]) 

有用。要使 anotherModel 成为公共属性,请将其写为

property ListModel anotherModel: ListModel {}
于 2011-07-30T12:39:37.720 回答