Results 1 to 3 of 3

Thread: How to fetch files one by one from FolderListModel in QML?

  1. #1
    Join Date
    Apr 2011
    Posts
    231
    Thanks
    141
    Thanked 6 Times in 5 Posts

    Default How to fetch files one by one from FolderListModel in QML?

    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
    Qt Code:
    1. import QtQuick 2.0
    2. import QtQuick.Dialogs 1.0 // FileDialog
    3. import Qt.labs.folderlistmodel 2.1 // FolderListModel
    4.  
    5. Item
    6. {
    7. id: head
    8. property url path
    9.  
    10. property int i: 0
    11.  
    12. height: 500; width: 500
    13. FileDialog
    14. {
    15. id: photoDirectoryFileDialog
    16. title: "Select the photo directory:"
    17. selectFolder: true
    18. visible: true
    19. height: parent.height; width: parent.width
    20. onAccepted: head.path = fileUrl
    21. }
    22.  
    23. ListView
    24. {
    25. FolderListModel
    26. {
    27. id: folderModel
    28. folder: photoDirectoryFileDialog.fileUrl
    29. nameFilters: ["*.jpg"]
    30. }
    31.  
    32. Component
    33. {
    34. id: fileDelegate
    35. Text { text: fileName }
    36. }
    37.  
    38. model: folderModel
    39. delegate: fileDelegate
    40. }
    41.  
    42. // Show photos
    43. Image
    44. {
    45. id: image
    46. source: ""
    47. }
    48.  
    49. MouseArea
    50. {
    51. anchors.fill: parent
    52. onClicked:
    53. {
    54. console.log ("fsdfsdf: " + i + " --- " + photoDirectoryFileDialog.fileUrl + "/" + folderModel.get (i, folderModel.fileName))
    55. image.source = photoDirectoryFileDialog.fileUrl + "/" + folderModel.get (i, folderModel.folder.fileName)
    56. i++
    57. }
    58. }
    59. }
    To copy to clipboard, switch view to plain text mode 

    ----------


    main.qml

    Qt Code:
    1. import QtQuick 2.4
    2. import QtQuick.Window 2.2
    3.  
    4. Window
    5. {
    6. id: rootWindow
    7. visible: true
    8. height: 700; width: height
    9.  
    10. PhotoViewer
    11. {
    12. height: rootWindow.height; width: rootWindow.width
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 


    Output:
    Qt Code:
    1. QML debugging is enabled. Only use this in a safe environment.
    2. qml: fsdfsdf: 0 --- file:///home/***/Pictures/Wallpapers/undefined
    3. qrc:/PhotoViewer.qml:43:5: QML Image: Cannot open: file:///home/***/Pictures/Wallpapers/undefined
    4. qml: fsdfsdf: 1 --- file:///home/***/Pictures/Wallpapers/undefined
    5. qml: fsdfsdf: 2 --- file:///home/***/Pictures/Wallpapers/undefined
    To copy to clipboard, switch view to plain text mode 

    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?

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to fetch files one by one from FolderListModel in QML?

    Look at the documentation of FolderListModel.get()
    The second argument is a string that tells get() which property of the entry you want.

    You are passing "undefined", since FolderListModel does not have a property "fileName" (in your log output) nor does string have a property "fileName" (in the next line).

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    TheIndependentAquarius (7th March 2016)

  4. #3
    Join Date
    Apr 2011
    Posts
    231
    Thanks
    141
    Thanked 6 Times in 5 Posts

    Default Re: How to fetch files one by one from FolderListModel in QML?

    Thanks, you reminded me that the problem was that I had forgotten that the second variable of the function get is a string.

    That means that the property `fileName` has to be passed in "", as shown below.

    Qt Code:
    1. console.log ("fsdfsdf: " + folderModel.count + " --- " + photoDirectoryFileDialog.fileUrl + "/" + folderModel.get (folderModel.count-1, "fileName"))
    2. image.source = photoDirectoryFileDialog.fileUrl + "/" + folderModel.get (folderModel.count-1, "fileName")
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. qt fetch the url of a html
    By sRB in forum Qt Programming
    Replies: 3
    Last Post: 17th December 2014, 10:39
  2. How to fetch pixmap from a label
    By Charvi in forum Qt Programming
    Replies: 1
    Last Post: 29th June 2012, 10:16
  3. FolderListModel custom property
    By WeX.tmpvar in forum Qt Quick
    Replies: 0
    Last Post: 18th February 2012, 07:57
  4. Fetch default values from MySQL?
    By arimaniac in forum Qt Programming
    Replies: 1
    Last Post: 1st February 2011, 21:36
  5. QSQLITE Unable to fetch row in create
    By xgoan in forum Newbie
    Replies: 3
    Last Post: 18th October 2006, 15:39

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.