我正在使用FolderListModel遇到麻烦。我试图只过滤特定目录的XML文件,并在ListView中显示它们。问题是,它只显示一个文件,而我在这个目录中有几个XML文件。 我试过其他类型的文件(txt,pdf),它从未在ListView中显示正确数量的文件。 这是我的代码,我做错了什么?
ListView {
id: listView1
x: 0
width: 288
height: 256
anchors.top: parent.top
anchors.topMargin: 16
anchors.horizontalCenter: parent.horizontalCenter
delegate: listviewdelegate
model: listviewmodel
clip: true;
}
FolderListModel{
id:listviewmodel
nameFilters: ["*.xml"]
showDirs: false
showDotAndDotDot: false
folder:"C:/Users/bg/Documents"//serializationpath
}
Component{
id:listviewdelegate
Text {
text: fileName
color: m_colorDefault
font.pixelSize: m_iFontSizeMin
anchors.verticalCenter: parent.verticalCenter
}
}
我们不能在ListView中使用FolderListModel吗? 谢谢你的帮助,
此致
修改 当我试图解决我的问题时,我注意到了Qt文档 不正确的文件夹属性。它表示默认情况下它是无效的网址,但如果我没有设置该文件夹,则会使用该应用程序的文件夹。 我试图用绝对路径设置文件夹属性:
FolderListModel {
id: listviewmodel
folder: "F:/QtDev/Sources.ScenarioEditor"
}
但它一直在使用应用程序的文件夹,而不会大肆宣传错误的路径。所以我在这里有点困惑......
编辑2: 我终于成功定位了正确的文件夹,但现在我面临着nameFilters属性的愚蠢行为......
这是一个片段:
FolderListModel {
id: listviewmodel
showDirs: false
//works fine and filters XML
// folder:"file:/F:/QtDev/Sources.ScenarioEditor"
// nameFilters: ["*.xml"]
//works fine but doesn't filter XML
folder:"file:/"+scenario.serializationPath
nameFilters: ["*.xml"]
}
scenario.serializationPath以我的用户文件夹为目标,这是我真正需要使用的文件夹。但在这种情况下,文件过滤不起作用:/
任何帮助都会非常感激,因为我暂时停留在这个问题上。
此致
答案 0 :(得分:1)
我在尝试使用具有动态folder
名称和nameFilter
的FolderListModel时遇到了类似的问题。我试图更新文件夹和/或过滤器,然后使用Component.onCompleted
刷新附加到模型的视图。填充模型似乎是一个异步操作,Component.onCompleted
调用没有准备好数据,所以作为一种解决方法,我触发了FolderListModel count
值的视图更新:
property int totalFileCount: folderListModel.count
onTotalFileCountChanged: {
console.log("total files: " + totalFileCount);
// **** Refresh your view here ****
}
FolderListModel {
id: folderListModel
folder: "" // This gets updated by another function
nameFilters: [ "*.png" ]
}