How to list all drives under Windows using QML

  • Hi, I am making a small file browser using a laboratory module "Qt.labs.folderlistmodel 2.1", it works fine except that it cannot list the drives under Windows.
    In C++, when setting QFileSystemModel::setRootPath(""); the drives are given. So is there a better way to work around with QML?

  • I am having trouble with this too, but I might have something that works now... In C++ you can list the drives with QInfoList infoList = QDir::drives();, but setting the folder of FolderListModel is kind of tricky. I am working with resources, so "/" will list the root of my resource file, and any invalid URL just lists the current directory. As a quick proof of concept I got it to work with the following:


    #include <QtCore>
    class Util: public QObject
        explicit Util(QObject *parent = 0);
        Q_INVOKABLE static QStringList drives();


    #include "util.h"
    #include <QDir>
    #include <QFileInfo>
    Util::Util(QObject *parent)
    : QObject(parent)
    QStringList Util::drives()
        QStringList ret;
        foreach (QFileInfo info, QDir::drives()) {
            ret << info.absolutePath();
        return ret;


    import QtQuick 2.0
    import Qt.labs.folderlistmodel 2.1
    Rectangle {
        width: 800
        height: 600
        visible: true
        property var drives: Util.drives()
        ListView {
            anchors.fill: parent
            FolderListModel {
                id: folderModel
                nameFilters: ["*"]
                folder: "file:" + drives[0]
            model: folderModel
            delegate: Component {
                id: fileDelegate
                Text { text: fileName }


    #include "util.h"
    #include <QGuiApplication>
    #include <QQuickView>
    #include <QQmlContext>
    int main(int argc, char *argv[])
    	QGuiApplication app(argc, argv);
    	/* QML class types */
    	qmlRegisterType<Util>("NotUsed", 1, 0,
    	QQuickView view(QUrl(QLatin1String("main.qml")));
    	view.rootContext()->setContextProperty("Util", new Util(&view));;
    	return app.exec();

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.