QStorageInfo signal when # volumes changes?
-
I want a signal when a storage volume comes online or goes offline, so i can update my gui. This is NOT about USB, it's about file systems (which may connect via protocols other than USB).
Basically i want to know when calling
QStorageInfo::mountedVolumes()would give me a different number than before, and some ID of what volume it is (was).on mac this corresponds to listening for
kEventVolumeMounted or kEventVolumeUnmounted
on windows listen forWM_DEVICECHANGE: DBT_DEVICEARRIVAL, DBT_DEVICEREMOVECOMPLETEI'd be find just doing THAT if i knew how to add cocoa or win32 API specific code to my app, so if you know how to do THAT that would help.
Whither this signal?
-
You can use Solid.
Solid::DeviceNotifier *notifier = Solid::DeviceNotifier::instance(); const auto checkVolumeChange = [](const QString& uid, bool added){ Solid::Device addedDevice(uid); if(!addedDevice.is<Solid::StorageVolume>()) return; qDebug() << "Volume " << addedDevice.displayName() << added ? QStringLiteral("Added") : QStringLiteral("Removed"); }; QObject::connect(notifier,&Solid::DeviceNotifier::deviceAdded, std::bind(checkVolumeChange,std::placeholders::_1,true)); QObject::connect(notifier,&Solid::DeviceNotifier::deviceRemoved, std::bind(checkVolumeChange,std::placeholders::_1,false)); -
does this "Solid" work on both mac and windows? Note i'm not developing for linux at all.
-
does this "Solid" work on both mac and windows? Note i'm not developing for linux at all.
@davecotter said in QStorageInfo signal when # volumes changes?:
does this "Solid" work on both mac and windows?
From https://api.kde.org/frameworks/solid/html/

-
so there's no way to do it within the existing Qt frameworks?
-
so there's no way to do it within the existing Qt frameworks?
@davecotter said in QStorageInfo signal when # volumes changes?:
the existing Qt frameworks?
Not that I know of.
Note that KDE and Qt have their destinies very tangled together, the KDE API maintainers are also maintainers within Qt, some classes made the jump from KDE API to Qt too (e.g. QConcatenateTablesProxyModel). KDE API is to Qt what Boost is to the C++ standard library
-
thanks for the reassurance, i was hesitant when i saw it was a 3rd party package. I'll give it a try!
-
I'm glad this question was posed (and answered)! I could have made use of this in the past, and hopefully will do so in the future.
Looks like the code is LGPL, too: https://invent.kde.org/frameworks/solid/-/blob/master/LICENSES/LicenseRef-KDE-Accepted-LGPL.txt