Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Get an element from FolderListModel



  • Hi all,
    I need to get a filename from a FolderListModel as in the following example:

    @
    ...
    ...
    Rectangle {
    width: 360
    height: 360

    MouseArea {
        anchors.fill: parent
        onClicked: {
            Qt.quit();
        }
        Image {
            id: immagine
            source: lista_immagini_model.get(0)
    
        }
    }
    
    FolderListModel {
        id: lista_immagini_model
        folder: "../../Immagini/"
        nameFilters: ["*.jpg"]
    }
    

    }
    ...
    ...
    @

    But :
    @
    lista_immagini_model.get(0)
    @
    doesn't exists...

    Is there a way to do that?



  • With a ListModel it works:
    @
    ...
    ...
    Image {
    id: immagine
    source: lista_immagini_model.get(0).fileName

        }
    

    ...
    ...
    ListModel {
    id: lista_immagini_model

        ListElement {
            fileName: "../../Immagini/DSC_0355_art.JPG"
        }
        ListElement {
            fileName: "../../Immagini/DSC_0366.JPG"
        }
        ListElement {
            fileName: "../../Immagini/IMG_0244.JPG"
        }
    }
    

    @

    How can I load the content of a FolderListModel in a ListModel?



  • Ok, suppose I want to create a model in C++ (Ex. QStringListModel) with the filenames I need.
    What should be the better way to access that model from QML?



  • Hello,

    ListModel has method called get(), but FolderListModel doesn't.

    I think the easiest way will be to extend current implementation of FolderListModel.

    You can see the source code "here":http://developer.qt.nokia.com/doc/qt-4.8/src-imports-folderlistmodel.html

    See the implementation of isFolder function and add another one that returns filename by given index.


Log in to reply