Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qt and Device Notifications
Forum Update on Monday, May 27th 2025

Qt and Device Notifications

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 2 Posters 1.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • webzoidW Offline
    webzoidW Offline
    webzoid
    wrote on last edited by webzoid
    #1

    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 the nativeEvent do not confirm this.

    Any help would be greatly appreciated.

    1 Reply Last reply
    1
    • webzoidW Offline
      webzoidW Offline
      webzoid
      wrote on last edited by
      #2

      Ok, I managed to resolve this...

      Turns out implementing a custom QAbstractNativeEventFilter and installing via the QApplication::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.

      1 Reply Last reply
      2
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        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.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        1
        • webzoidW Offline
          webzoidW Offline
          webzoid
          wrote on last edited by
          #4

          @SGaist Yes, I would certainly consider that. How would one go about accomplishing such a task?

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            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.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            1

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved