Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to list all drives under Windows using QML
Qt 6.11 is out! See what's new in the release blog

How to list all drives under Windows using QML

Scheduled Pinned Locked Moved QML and Qt Quick
3 Posts 3 Posters 1.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jiangcaiyang
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      Moore Tides
      wrote on last edited by Moore Tides
      #2

      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:

      util.h

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

      util.cpp

      #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;
      }
      

      main.qml

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

      main.cpp

      #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,
      				      "Util_Class");
      	QQuickView view(QUrl(QLatin1String("main.qml")));
      	view.rootContext()->setContextProperty("Util", new Util(&view));
      	view.show();
      	return app.exec();
      }
      
      S 1 Reply Last reply
      0
      • M Moore Tides

        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:

        util.h

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

        util.cpp

        #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;
        }
        

        main.qml

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

        main.cpp

        #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,
        				      "Util_Class");
        	QQuickView view(QUrl(QLatin1String("main.qml")));
        	view.rootContext()->setContextProperty("Util", new Util(&view));
        	view.show();
        	return app.exec();
        }
        
        S Offline
        S Offline
        sagarbade
        wrote on last edited by
        #3

        @Moore-Tides Hello As you take Folderlistmodel as model it returns all files from drives[0] (C:/) ..try this
        //main.cpp
        GetDrives myDrives;
        QQmlContext *ctxtforDrives=engine.rootContext();
        QStringList Driveslist=myDrives.drives()
        ctxtforDrives->setContextProperty("DrivesModel", QVariant::fromValue(Driveslist));
        //qml
        set model=DrivesModel

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved