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
============
Code:
#ifndef USBDETECTION_H
#define USBDETECTION_H
#include <QAbstractNativeEventFilter>
#include <QProcess>
#include <QMap>
class UsbDetection : public QAbstractNativeEventFilter
{
public:
UsbDetection();
virtual bool nativeEventFilter
(const QByteArray &eventType,
void *message,
long *) Q_DECL_OVERRIDE;
};
#endif // USBDETECTION_H
UsbDetection.cpp
=============
Code:
#include <QByteArray>
#include <windows.h>
#include <dbt.h>
#include "UsbDetection.h"
#include <QDebug>
#include <QDir>
UsbDetection::UsbDetection()
: QAbstractNativeEventFilter()
/*, mWmicProcess(new QProcess())
, mIsDevInsertion(FALSE)
, mDriveToDevNameMap(new QMap<QString, TDriveToMediaPair>())*/
{
qDebug() << "In constructor()";
}
bool UsbDetection
::nativeEventFilter(const QByteArray &eventType,
void *pMessage,
long* pResult
) //Q_DECL_OVERRIDE {
qDebug() << " In native events" ;
Q_UNUSED(eventType);
Q_UNUSED(pResult);
auto pWindowsMessage = static_cast<MSG*>(pMessage);
auto wParam = pWindowsMessage->wParam;
qDebug() << "wParam = " << wParam;
if (wParam == DBT_DEVICEARRIVAL || wParam == DBT_DEVICEREMOVECOMPLETE) {
qDebug() << "USB plug/unplug = " << wParam;
....................
}
return true;
}
main.cpp
========
Code:
#include <QApplication>
#include <QDebug>
#include <QDir>
#include <QAbstractEventDispatcher>
#include "UsbDetection.h"
int main(int argc, char *argv[])
{
UsbDetection UsbDetection;
app.installNativeEventFilter(&UsbDetection);
return app.exec();
}
Output :
=======
In constructor()
In native events
wParam = 0
In native events
wParam = 0
In native events
wParam = 0
In native events
wParam = 0
Re: QAbstractNativeEventFilter::nativeEventFilter not receiving DBT_DEVICEARRIVAL
Are you sure your application will work at all with you filtering away all native events?
Cheers,
_
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?
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,
_
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
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,
_