Is there a Qt solution for detecting USB events (insertion and removal)?
As already pointed out, Qt itself does not provide such a module. There is however a user-made class called QDeviceWatcher
. I have no experience with it personally and it is not updated regularly but you could give it a try.
You can find the forum post here and the git repository here.
How to bind to connect/disconnect USB device event in windows 7/8/10 from Qt
Thanks to @kunif I've found solution. So to listen to Windows messages you need to add your own EventFilter by inheriting QAbstractNativeEventFilter like this:
#include <QAbstractNativeEventFilter>
#include <QObject>
class DeviceEventFilter : public QObject, public QAbstractNativeEventFilter
{
Q_OBJECT
public:
DeviceEventFilter();
bool nativeEventFilter(const QByteArray &eventType, void *message, long *) override;
signals:
void serialDeviceChanged();
};
And filter the message you need WM_DEVICECHANGE:
#include <windows.h>
#include <dbt.h>
bool DeviceEventFilter::nativeEventFilter(const QByteArray &eventType, void *message, long *) {
if (eventType == "windows_generic_MSG") {
MSG *msg = static_cast<MSG *>(message);
if (msg->message == WM_DEVICECHANGE) {
if (msg->wParam == DBT_DEVICEARRIVAL || msg->wParam == DBT_DEVICEREMOVECOMPLETE) {
// connect to this signal to reread available ports or devices etc
emit serialDeviceChanged();
}
}
}
return false;
}
And somewhere in your code, where you have access to DeviceEventFilter
object add this line:
qApp->installNativeEventFilter(&devEventFilterObj);
Or in main.cpp
:
QApplication app(argc, argv);
app.installNativeEventFilter(&devEventFilterObj);
All gratitude to @kunif !
Detecting USB mouse. Qt on Embedded Linux
Qt itself does not provide such a module. There is a user-made class called QDeviceWatcher, you could give it a try.
If you are using QtEmbedded Linux, you have a folder with all USB devices info (vendor id, etc), probably in /proc/scsi/usb-storage
file. So you probably could use QFileSystemWatcher to handle modification in this folder.
See: Qt Centre Post for more detail.
How to get path to USB drive on Linux in Qt?
I found this code to do exactly what I need:
foreach (const QStorageInfo &storage, QStorageInfo::mountedVolumes()) {
qDebug() << storage.rootPath();
if (storage.isReadOnly())
qDebug() << "isReadOnly:" << storage.isReadOnly();
qDebug() << "name:" << storage.name();
qDebug() << "fileSystemType:" << storage.fileSystemType();
qDebug() << "size:" << storage.bytesTotal()/1000/1000 << "MB";
qDebug() << "availableSize:" << storage.bytesAvailable()/1000/1000 << "MB";
}
Output:
"/run"
name: ""
fileSystemType: "tmpfs"
size: 6748 MB
availableSize: 6729 MB
"/"
name: ""
fileSystemType: "ext4"
size: 64370 MB
availableSize: 22236 MB
"/run/lock"
name: ""
fileSystemType: "tmpfs"
size: 5 MB
availableSize: 5 MB
"/home"
name: ""
fileSystemType: "ext4"
size: 183169 MB
availableSize: 27305 MB
"/run/user/1000"
name: ""
fileSystemType: "tmpfs"
size: 6748 MB
availableSize: 6748 MB
"/media/superuser/Backups"
name: "Backups"
fileSystemType: "ext4"
size: 252113 MB
availableSize: 133173 MB
"/media/superuser/Data"
name: "Data"
fileSystemType: "ext4"
size: 732123 MB
availableSize: 694298 MB
"/media/superuser/BackupWD"
name: "BackupWD"
fileSystemType: "fuseblk"
size: 209713 MB
availableSize: 13144 MB
"/media/superuser/WDSpace"
name: "WDSpace"
fileSystemType: "fuseblk"
size: 790484 MB
availableSize: 582583 MB
How can I detect a USB disconnect event? (Windows, .NET C# application)
I ended up using this: http://msdn.microsoft.com/en-us/library/aa363480(VS.85).aspx
And followed this: http://www.codeproject.com/KB/system/DriveDetector.aspx
Related Topics
How to Know Whether a Copy-On-Write Page Is an Actual Copy
How to Open a File in Assembler and Modify It
Packet Sniffing Using Raw Sockets in Linux in C
How to Replace Single Quotes with Another Character in Sed
What Is Kernel Section Mismatch
Error: Ld.So: Object 'Libgtk3-Nocsd.So.0' from Ld_Preload Cannot Be Preloaded
Shell Script Change Directory with Variable
Run Docker in Ubuntu Live Disk
How to "Git Pull" - Host Key Verification Failed
Linux Command to Replace String in Large File with Another String
Poll(2) Doesn't Empty the Event Queue
How to Connect to Jenkins Server (Amazon Linux Ami)
Bash Printf %Q Invalid Directive
Apache Pig: Load a File That Shows Fine Using Hadoop Fs -Text