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

QtQuick 2.0: How to get the content filenames of a directory?



  • I need to get a list of names of all the files contained in the directory. How can I do this? Mean while invented just such a dirty hack:
    [code]import QtQuick 2.0
    import Qt.labs.folderlistmodel 1.0
    Item {
    id: filenames
    signal done()
    property var content: new Array(0)

    ListView {
        visible: false
        model: FolderListModel {
            id: folderModel
            folder: "frames"
        }
        delegate: Item {
            Component.onCompleted: {
                content.push(fileName)
                if (index == folderModel.count - 1) done()
            }
        }
    }
    

    }[/code]



  • Please don't do this.
    Instantiating delegates to do this sort of thing is evil.

    You should instead define a "singleton type":http://qt-project.org/doc/qt-5.0/qqmlengine.html#qmlRegisterSingletonType-2 which has a Q_INVOKABLE function returning a QStringList, containing the directory contents (something like "Q_INVOKABLE QStringList directoryContents(const QString &path) const") which uses QDirIterator or something similar to enumerate the contents of the directory.

    Cheers,
    Chris.



  • how comes that Qt.labs.folderlistmodel is still in labs ?



  • C++ Backend (Singleton) is indeed much more preferable for this and not QML


Log in to reply