1) İlk olarak headerlar eklenir
#include <windows.h>
#include <dbt.h>
2)QWidget için virtual fonksiyonu override ederiz herhangi bir usb eventi oluştuğunda artık yakalayabiliriz
bool DT_IFVPLAYER::nativeEvent(const QByteArray& eventType, void* message, long* result){
Q_UNUSED(eventType);
MSG* msg = reinterpret_cast<MSG*>(message);
int msgType = msg->message;
if (msgType == WM_DEVICECHANGE)
{
PDEV_BROADCAST_HDR lpdb = (PDEV_BROADCAST_HDR)msg->lParam;
switch (msg->wParam)
{
case DBT_DEVICETYPESPECIFIC:
{ qDebug() << "DBT_DEVICETYPESPECIFIC ";
break;
}
case DBT_DEVICEARRIVAL:
if (lpdb->dbch_devicetype == DBT_DEVTYP_VOLUME)
{
PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)lpdb;
if (lpdbv->dbcv_flags == 0)
{
check_drive();
}
}
qDebug() << "DBT_DEVICEARRIVAL";
break;
case DBT_DEVICEREMOVECOMPLETE:
if (lpdb->dbch_devicetype == DBT_DEVTYP_VOLUME)
{
PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)lpdb;
if (lpdbv->dbcv_flags == 0)
{
check_drive();
}
}
qDebug() << "DBT_DEVICEREMOVECOMPLETE";
break;
}
}
return false;
}