model1=new QFileSystemModel();model1->setRootPath(QDir::currentPath());model1->setFilter(QDir::AllDirs | QDir::Files |QDir::NoDotAndDotDot);model1->setReadOnly(false); ui->treeview->setModel(model1)
C++ (Qt)#include <QtDBus>#define UD_DBUS_SERVICE "org.freedesktop.UDisks"#define UD_DBUS_PATH "/org/freedesktop/UDisks"#define UD_DBUS_INTERFACE_DISKS "org.freedesktop.UDisks"#define UD_DBUS_INTERFACE_DISKS_DEVICE "org.freedesktop.UDisks.Device"#define UD_DBUS_DEVICES_PREFIX "/org/freedesktop/UDisks/devices/" void DeviceManager::getData(){ QDBusInterface* UDisksInterface = new QDBusInterface(UD_DBUS_SERVICE, UD_DBUS_PATH, UD_DBUS_INTERFACE_DISKS, QDBusConnection::systemBus(), this); QDBusReply <QList <QDBusObjectPath> > objectPathList = UDisksInterface->call("EnumerateDevices"); if (objectPathList.isValid()) { foreach (QDBusObjectPath pathDevice, objectPathList.value()) { QDBusInterface deviceInterface(UD_DBUS_SERVICE, pathDevice.path(), UD_DBUS_INTERFACE_DISKS_DEVICE, QDBusConnection::systemBus(), this); QVariant idUsage = deviceInterface.property("IdUsage"); if (idUsage.isValid()) { if (idUsage.toString() == "filesystem") { devicePathList << pathDevice.path().section('/', -1); mountPathList << deviceInterface.property("DeviceMountPaths").toString(); QVariant idType = deviceInterface.property("IdType"); if (idType.isValid()) fileSystemList << idType.toString(); else fileSystemList << ""; QVariant idLabel = deviceInterface.property("IdLabel"); if (idLabel.isValid()) { if (!idLabel.toString().isEmpty()) { labelDiskList << idLabel.toString(); diskNameList << idLabel.toString(); } else { labelDiskList << ""; diskNameList << devicePathList.last(); } } QVariant deviceIsHard = deviceInterface.property("DeviceIsSystemInternal"); if (deviceIsHard.isValid()) { if (deviceIsHard.toBool()) { typeDiskList << "drive-harddisk"; } else { QVariant deviceIsDrive = deviceInterface.property("DeviceIsDrive"); if (deviceIsDrive.isValid()) { if (deviceIsDrive.toBool()) { QVariant driveMediaCompatibility = deviceInterface.property("DriveMediaCompatibility"); if (driveMediaCompatibility.isValid()) { QStringList mediaTypes = driveMediaCompatibility.toStringList(); if (!mediaTypes.filter("optical").isEmpty()) typeDiskList << "media-optical"; else if (!mediaTypes.filter("flash").isEmpty()) typeDiskList << "drive-removable-media-usb-pendrive"; else if (!mediaTypes.filter("floppy").isEmpty()) typeDiskList << "media-floppy"; else typeDiskList << "drive-harddisk"; } } else { if (deviceInterface.property("DriveConnectionInterface").toString() == "usb") typeDiskList << "drive-removable-media-usb-pendrive"; else typeDiskList << "drive-harddisk"; } } } } } } }