QML之FolderListModel用法
FolderListModel是QT提供的一个可以访问本地系统文件夹内容的组件,它可以将获取到的信息提供给别的组件使用。
注意:使用前需要导入Qt.labs.folderlistmodel模块。
Rectangle {
height: 400
width: 400
anchors.centerIn: parent
FolderListModel
{
id: foldermodel
folder: "./PageObc" //需要解析的文件夹
showDirs: true //是否显示文件夹。默认为真
showDotAndDotDot: false //如果为真,"." and ".."目录被包含在model中,否则被排除。默认为假
nameFilters: ["*.qml","*.png"] //筛选过滤作用,注意的是目录不会被过滤排除
sortField: FolderListModel.Type //设置排序模式,是一个枚举值,下面进行讲解
showOnlyReadable: true
sortReversed: false //如果为真,逆转排序顺序。默认为假
}
Component {
id: fileDelegate
Text {
text: index + "/" + foldermodel.count + " " + fileName //count 过滤后的文件个数
color: foldermodel.isFolder(index)? "red" : "blue" //判断是否文件夹
font.pixelSize: 20
}
}
ListView{
width: 200
height: 200
anchors.centerIn: parent
model: foldermodel
delegate: fileDelegate
}
}
补充说明:
sortField :enumeration
sortField包含用于排序的域,它可能是下面的一个值:
Unsorted - 不排序
Name - 按文件名排序
LastModified- 按修改时间排序
Size - 按文件尺寸排序
Type - 按文件类型排序(扩展名)
folder属性指明访问的文件夹。文件夹中文件和目录的信息经由model接口提供。该model组件经下面的标识访问名字和路径:
fileName
filePath
fileURL(since Qt 5.2)
fileBaseName
fileSuffix
fileSize
fileModified
fileAccessed
fileIsDir
另外,附加的方法isFolder(),能用来区分入口是文件还是目录
注意:使用前需要导入Qt.labs.folderlistmodel模块。
Rectangle {
height: 400
width: 400
anchors.centerIn: parent
FolderListModel
{
id: foldermodel
folder: "./PageObc" //需要解析的文件夹
showDirs: true //是否显示文件夹。默认为真
showDotAndDotDot: false //如果为真,"." and ".."目录被包含在model中,否则被排除。默认为假
nameFilters: ["*.qml","*.png"] //筛选过滤作用,注意的是目录不会被过滤排除
sortField: FolderListModel.Type //设置排序模式,是一个枚举值,下面进行讲解
showOnlyReadable: true
sortReversed: false //如果为真,逆转排序顺序。默认为假
}
Component {
id: fileDelegate
Text {
text: index + "/" + foldermodel.count + " " + fileName //count 过滤后的文件个数
color: foldermodel.isFolder(index)? "red" : "blue" //判断是否文件夹
font.pixelSize: 20
}
}
ListView{
width: 200
height: 200
anchors.centerIn: parent
model: foldermodel
delegate: fileDelegate
}
}
补充说明:
sortField :enumeration
sortField包含用于排序的域,它可能是下面的一个值:
Unsorted - 不排序
Name - 按文件名排序
LastModified- 按修改时间排序
Size - 按文件尺寸排序
Type - 按文件类型排序(扩展名)
folder属性指明访问的文件夹。文件夹中文件和目录的信息经由model接口提供。该model组件经下面的标识访问名字和路径:
fileName
filePath
fileURL(since Qt 5.2)
fileBaseName
fileSuffix
fileSize
fileModified
fileAccessed
fileIsDir
另外,附加的方法isFolder(),能用来区分入口是文件还是目录