Intention of the following code is to let the user select a folder of photos, and then display those photos one by one.
PhotoViewer.qml
import QtQuick 2.0
import QtQuick.Dialogs 1.0 // FileDialog
import Qt.labs.folderlistmodel 2.1 // FolderListModel
Item
{
id: head
property url path
property int i: 0
height: 500; width: 500
FileDialog
{
id: photoDirectoryFileDialog
title: "Select the photo directory:"
selectFolder: true
visible: true
height: parent.height; width: parent.width
onAccepted: head.path = fileUrl
}
ListView
{
FolderListModel
{
id: folderModel
folder: photoDirectoryFileDialog.fileUrl
nameFilters: ["*.jpg"]
}
Component
{
id: fileDelegate
Text { text: fileName }
}
model: folderModel
delegate: fileDelegate
}
// Show photos
Image
{
id: image
source: ""
}
MouseArea
{
anchors.fill: parent
onClicked:
{
console.log ("fsdfsdf: " + i + " --- " + photoDirectoryFileDialog.fileUrl + "/" + folderModel.get (i, folderModel.fileName))
image.source = photoDirectoryFileDialog.fileUrl + "/" + folderModel.get (i, folderModel.folder.fileName)
i++
}
}
}
main.qml
import QtQuick 2.4
import QtQuick.Window 2.2
Window
{
id: rootWindow
visible: true
height: 700; width: height
PhotoViewer
{
height: rootWindow.height; width: rootWindow.width
}
}
Output:
QML debugging is enabled. Only use this in a safe environment.
qml: fsdfsdf: 0 --- file:///home/***/Pictures/Wallpapers/undefined
qrc:/PhotoViewer.qml:43:5: QML Image: Cannot open: file:///home/***/Pictures/Wallpapers/undefined
qml: fsdfsdf: 1 --- file:///home/***/Pictures/Wallpapers/undefined
qml: fsdfsdf: 2 --- file:///home/***/Pictures/Wallpapers/undefined
As you can see in the output, I am receiving "undefined" as file name in the output. How to fetch files one by one from FolderListModel in QML?