2

How I can list drives or mounted partitions using qt? I tried to use:

foreach( QFileInfo drive, QDir::drives() )
       {
         qDebug() << "Drive: " << drive.absolutePath();
       }

but it shows just root drive. I also noticed that length of QDir::drives() is 1 but QDir::Drives is 4.

ahaw
  • 221
  • 2
  • 10

3 Answers3

5

You need to use platform specific code. And, please, read the docs!

Returns a list of the root directories on this system.

On Windows this returns a list of QFileInfo objects containing "C:/", "D:/", etc. On other operating systems, it returns a list containing just one root directory (i.e. "/").

peppe
  • 21,934
  • 4
  • 55
  • 70
  • I didn't notice that this is ignored on Linux. But still I cannot find anything about partitions/disks different than root. Would you be so kind and show me where I should look? – ahaw Jun 07 '13 at 11:28
  • Under Linux you can use `getmntent` and friends to open `/etc/mtab` and analyze the information in there. – peppe Jun 07 '13 at 12:05
5

You can use /etc/mtab file to obtain a mountpoints list.

QFile file("/etc/mtab");
if (file.open(QFile::ReadOnly)) {
  QStringList mountpoints;
  while(true) {
    QStringList parts = QString::fromLocal8Bit(file.readLine()).trimmed().split(" ");
    if (parts.count() > 1) {
      mountpoints << parts[1];
    } else {
      break;
    }
  }
  qDebug() << mountpoints;
}

Output on my machine:

("/", "/proc", "/sys", "/sys/fs/cgroup", "/sys/fs/fuse/connections", "/sys/kernel/debug", "/sys/kernel/security", "/dev", "/dev/pts", "/run", "/run/lock", "/run/shm", "/run/user", "/media/sf_C_DRIVE", "/media/sf_C_DRIVE", "/media/sf_D_DRIVE", "/run/user/ri/gvfs")

Note that QFile::atEnd() always returns true for this file, so I didn't use it in my code.

QDir::Drives is 4 according to the documentation. It's static integer value of enum item, it doesn't show anything and you shouldn't care about it in most cases. QDir::drives() contains exactly one item (for root filesystems) when executed on Linux.

Pavel Strakhov
  • 39,123
  • 5
  • 88
  • 127
  • Thanks, I expected something without access to files but it is good enough for me:) – ahaw Jun 07 '13 at 22:10
  • Use `/etc/mtab` and **not** `/proc/mounts` for ordinary user programs! `/proc/mounts` lists all mountpoints in chronological order, even those overridden -- as you can see, in your case `/` is reported *twice*! That information is useful only for system programs. – peppe Jun 09 '13 at 08:15
  • Also this is going to break horribly as it's not handlng whitespace conversions properly (trivia: what do you get in `/etc/mtab` or `/proc/mounts` if the mount point has a space in its name? Answer: search for "whitespace" in [getmntent man page](http://linux.die.net/man/3/getmntent)). You really **need** to use `getmntent`, as I said in my answer. – peppe Jun 09 '13 at 08:24
3

Qt 5.4+

You can use QStorageInfo class in Qt 5.4+ as follow:

foreach (const QStorageInfo &storage, QStorageInfo::mountedVolumes()) {
    if (storage.isValid() && storage.isReady()) {
        if (!storage.isReadOnly()) {
            // ...
        }
    }
}

more info

S.M.Mousavi
  • 5,013
  • 7
  • 44
  • 59