Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a Qt solution for detecting USB events (insertion and removal)?

Tags:

c++

qt

usb

As some other people already asked here some years ago, I want to be able to detect in a Qt application when a pendrive/datatraveler is plugged and when it is unplugged in my system (both in Linux Ubuntu and Embedded Linux).

I searched in SO and I noticed that most answers not only were very outdated (from 2010 for example), but all solutions somehow involved "non-Qt solutions" to these problem: either platform specific or usage of external libraries such as udev and DBus.

What I want to know first of all is if there is a specific Qt solution for this, i.e. a way to do this without requiring adding external libs or platform specific codes, and how could I use it. Preferentially it should already be available in Qt 4.8. If there is no Qt solution, then a Qt-based library would be acceptable.

Add: Search results:

  • USB Programming with Qt: Suggests libusb and WinUSB API
  • Detecting USB notification in Qt on windows: Windows only
  • How do I detect usb drive insertion in Linux?: Suggests libudev
  • How to get USB notifications under linux / Qt?: Suggests DBus
  • How to know when a new USB storage device is connected in Qt?: Windows only
  • How to detect USB device disconnect under Linux/Qt/C++: Linux only, suggests HAL, DeviceKit and udev
  • Is there a C++ cross platform USB library?: Suggests libusbx
like image 778
Momergil Avatar asked Apr 02 '15 13:04

Momergil


3 Answers

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.

like image 108
Bowdzone Avatar answered Nov 16 '22 07:11

Bowdzone


No such API in Qt, but you can go on with some platform-wise implementations of libusb, which is quite awesome.

like image 6
Ian P Badtrousers Avatar answered Nov 16 '22 05:11

Ian P Badtrousers


Just one more tip:

libusbx was a fork of libusb, a library that provides generic access to USB devices. As of 2014.01.26, this project has been fully merged back into libusb and is being discontinued. Since there will be no further releases of libusbx, you are strongly encouraged to switch to using libusb. -----from https://web.archive.org/web/20150519101032/http://libusbx.org/

like image 4
logic Avatar answered Nov 16 '22 07:11

logic