Search code examples
qtqtquick2qt-quick

QtQuick - FolderListModel not showing all the files


I am facing troubles using FolderListModel. I am trying to filter only XML files of a specific directory and display them in a ListView. The problem is, it only displays one file whereas I have several XML files in this directory. I tried with other types of files (txt, pdf) and it never displays the correct amount of files in the ListView. Here is my code, what am I doing wrong?

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
            }

        }

Can't we use a FolderListModel inside a ListView? Thanks for your help,

Regards

Edit: As I am trying to solve my issue, I have noticed that the Qt documentation isn't correct for the folder property. It says it is an invalid URL by default, but if I don't set the folder, it uses the application's folder. I tried to set the folder property with absolute path:

FolderListModel {
                id: listviewmodel
                folder: "F:/QtDev/Sources.ScenarioEditor"
            }

But it keeps using the application's folder, without yelling about a wrong path. So I am a bit confused here...

Edit 2: I finally succeeded in targeting the right folder, but now I am facing a stupide behavior of the nameFilters property...

Here is a snippet:

                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"]


}

The scenario.serializationPath targets my user folder, which is the one I truly need to use. But in that case, the file filtering doesn't not work :/

Any help will be much appreciated, as I am stuck on this problem for a while.

Regards


Solution

  • I stumbled across a similar issue while trying to use a FolderListModel with a dynamic folder name and nameFilter. I was trying to update the folder and/or filter, then refresh a view that is attached to the model using Component.onCompleted. Populating the model seems to be an asynchronous operation and the data isn't ready by the Component.onCompleted call, so as a workaround I triggered the view update on the FolderListModel count value:

    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" ]
    }