Unsolved Qt and Device Notifications
-
Can anyone share any pearls of wisdom, please, relating to registering for device notifications in Qt 5.11.1.
Having taken some working MFC code and ported it across to Qt, I can't for the life of me get my device to trigger any notifications.
My code is thus:
static GUID GUID_DFU = { 0x3fe809ab, 0xfb91, 0x4cb5, { 0xa6, 0x43, 0x69, 0x67, 0x0d, 0x52, 0x36, 0x6e } }; void MainWindow::registerForDeviceNotifications() { HWND hWnd = (HWND)this->winId(); DEV_BROADCAST_DEVICEINTERFACE filter; memset(&filter, 0, sizeof(DEV_BROADCAST_DEVICEINTERFACE)); filter.dbcc_size = sizeof(filter); filter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE; // Register specifically for the DFU GUID device notifications filter.dbcc_classguid = GUID_DFU; // Register for the DFU device notifications RegisterDeviceNotification(hWnd, &filter, DEVICE_NOTIFY_WINDOW_HANDLE); } bool MainWindow::nativeEvent(const QByteArray &eventType, void *message, long *result) { _DEV_BROADCAST_HEADER* hdr = nullptr; // Get the MSG stuct MSG* msg = static_cast<MSG *>(message); UINT type = msg->message; WPARAM wParam = msg->wParam; qDebug() << "type:" << type; qDebug() << "wParam:" << wParam; // THIS NEVER HAPPENS // THE ONLY THING THAT SEEMS TO BE CORRECT IS THAT wParam == WM_DEVICECHANGE if (type == DBT_DEVICEARRIVAL) { qDebug() << "APP: Device Arrival"; hdr = (_DEV_BROADCAST_HEADER *)msg->lParam; if (hdr->dbcd_devicetype == DBT_DEVTYP_DEVICEINTERFACE) { refreshDevices(); } } else if (type == DBT_DEVICEREMOVECOMPLETE) { qDebug() << "APP: Device Remove Complete"; } return false; }
The only thing I can seem to get to work is when I check
wParam == WM_DEVICECHANGE
but every other example I have seen using thenativeEvent
do not confirm this.Any help would be greatly appreciated.
-
Ok, I managed to resolve this...
Turns out implementing a custom
QAbstractNativeEventFilter
and installing via theQApplication::installNativeEventFilter
function does the trick!I haven't seen many examples of this around so I'll post back my complete working code incase it is useful to someone else.
-
Hi,
Glad you found out and thanks for sharing !
Rather than just posting your example here, would you consider adding it to the Qt documentation ? That would allow more people to easily find it.
-
@SGaist Yes, I would certainly consider that. How would one go about accomplishing such a task?
-
You can start by taking a look at the gerrit introduction and the related article linked at the top.
I know it can look a bit scary but don't worry, it's a pretty thorough document to get you nicely started. Then you can clone the
qtbase
module and modify the QAbstractNativeEventFilter documentation. From a quick look, there are already code snippet for Linux and macOS that you can take inspiration from.