Results 1 to 6 of 6

Thread: QAbstractNativeEventFilter::nativeEventFilter not receiving DBT_DEVICEARRIVAL

  1. #1
    Join Date
    Jul 2009
    Posts
    5
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default QAbstractNativeEventFilter::nativeEventFilter not receiving DBT_DEVICEARRIVAL

    Hi All,

    I am trying to get a USb insertion and removal event in a non-GUI class but with much success. I am not able to get the arrival and removal events. Can anyone lend some pointers. My code looks like -


    Usbdetection.h
    ============

    Qt Code:
    1. #ifndef USBDETECTION_H
    2. #define USBDETECTION_H
    3.  
    4. #include <QAbstractNativeEventFilter>
    5. #include <QProcess>
    6. #include <QMap>
    7.  
    8. class UsbDetection : public QAbstractNativeEventFilter
    9. {
    10.  
    11. public:
    12. UsbDetection();
    13. virtual bool nativeEventFilter(const QByteArray &eventType, void *message, long *) Q_DECL_OVERRIDE;
    14. };
    15.  
    16.  
    17. #endif // USBDETECTION_H
    To copy to clipboard, switch view to plain text mode 


    UsbDetection.cpp
    =============
    Qt Code:
    1. #include <QByteArray>
    2. #include <windows.h>
    3. #include <dbt.h>
    4. #include "UsbDetection.h"
    5. #include <QDebug>
    6. #include <QDir>
    7.  
    8. UsbDetection::UsbDetection()
    9. : QAbstractNativeEventFilter()
    10. /*, mWmicProcess(new QProcess())
    11. , mIsDevInsertion(FALSE)
    12. , mDriveToDevNameMap(new QMap<QString, TDriveToMediaPair>())*/
    13. {
    14. qDebug() << "In constructor()";
    15. }
    16.  
    17. bool UsbDetection::nativeEventFilter(const QByteArray &eventType, void *pMessage, long* pResult) //Q_DECL_OVERRIDE
    18. {
    19. qDebug() << " In native events" ;
    20. Q_UNUSED(eventType);
    21. Q_UNUSED(pResult);
    22.  
    23. auto pWindowsMessage = static_cast<MSG*>(pMessage);
    24. auto wParam = pWindowsMessage->wParam;
    25. qDebug() << "wParam = " << wParam;
    26.  
    27. if (wParam == DBT_DEVICEARRIVAL || wParam == DBT_DEVICEREMOVECOMPLETE) {
    28. qDebug() << "USB plug/unplug = " << wParam;
    29. ....................
    30.  
    31. }
    32. return true;
    33. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    ========
    Qt Code:
    1. #include <QApplication>
    2.  
    3. #include <QDebug>
    4. #include <QDir>
    5. #include <QAbstractEventDispatcher>
    6.  
    7. #include "UsbDetection.h"
    8.  
    9. int main(int argc, char *argv[])
    10. {
    11. QCoreApplication app(argc, argv);
    12.  
    13. UsbDetection UsbDetection;
    14. app.installNativeEventFilter(&UsbDetection);
    15. return app.exec();
    16. }
    To copy to clipboard, switch view to plain text mode 


    Output :
    =======
    In constructor()
    In native events
    wParam = 0
    In native events
    wParam = 0
    In native events
    wParam = 0
    In native events
    wParam = 0
    Last edited by anda_skoa; 10th May 2016 at 12:40. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QAbstractNativeEventFilter::nativeEventFilter not receiving DBT_DEVICEARRIVAL

    Are you sure your application will work at all with you filtering away all native events?

    Cheers,
    _

  3. #3
    Join Date
    Jul 2009
    Posts
    5
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QAbstractNativeEventFilter::nativeEventFilter not receiving DBT_DEVICEARRIVAL

    This app is required just to put divide USB events. I am not directing anything else from the app. Is that what you wanted to know?

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QAbstractNativeEventFilter::nativeEventFilter not receiving DBT_DEVICEARRIVAL

    What i mean is: you are obviously getting native events but you filter them out.
    Are you sure the base implementation in QCoreApplication doesn't need them?

    Cheers,
    _

  5. #5
    Join Date
    Jul 2009
    Posts
    5
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QAbstractNativeEventFilter::nativeEventFilter not receiving DBT_DEVICEARRIVAL

    if you are talking about the return value from nativeEvent, then I have tried with false. But I still don't get the usb insertion/removal events

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QAbstractNativeEventFilter::nativeEventFilter not receiving DBT_DEVICEARRIVAL

    Hmm, then maybe it is necessary to register for these events by calling some Windows API?

    Also maybe check if libusb can deliver those notifications or if using a Qt hardware managemenSolid is a workable option.

    Cheers,
    _

Similar Threads

  1. Replies: 0
    Last Post: 27th May 2015, 16:27
  2. help with QAbstractNativeEventFilter
    By alee in forum Newbie
    Replies: 1
    Last Post: 2nd October 2013, 22:39
  3. Receiving Streaming frames
    By ^NyAw^ in forum Qt Programming
    Replies: 2
    Last Post: 25th May 2012, 09:14
  4. receiving XButtonEvent but not QMouseEvent
    By vivek.m in forum Qt Programming
    Replies: 0
    Last Post: 17th August 2010, 12:46
  5. mouseMoveEvent receiving zero's
    By JohnTh in forum Newbie
    Replies: 2
    Last Post: 11th September 2009, 17:31

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.