I have also tried to install an event filter for CD arrival notification and could not
get it to work. Here is what i did :
_________ MFrame.h_________________
#include <qt_windows.h>
{
Q_OBJECT
public:
MFrame();
protected :
bool winEvent(MSG *msg,long * result);
signals:
void CD_Removed
(QChar tchar
);
void CD_Arrived
(QChar tchar
);
};
#include <qt_windows.h>
class MFrame : public QWidget
{
Q_OBJECT
public:
MFrame();
protected :
bool winEvent(MSG *msg,long * result);
signals:
void CD_Removed(QChar tchar);
void CD_Arrived(QChar tchar);
};
To copy to clipboard, switch view to plain text mode
________ MFrame.cpp ____________________
#include <QtCore>
#include <QtGui>
#include <QtDebug>
#include "mframe.h"
#include <dbt.h>
static char FirstDriveFromMask (ULONG unitmask)
{
char i;
for (i = 0; i < 26; ++i)
{
if (unitmask & 0x1)
break;
unitmask = unitmask >> 1;
}
return (i + 'A');
}
bool MFrame::winEvent(MSG * msg, long * result)
{
int msgType = msg->message;
if(msgType == WM_DEVICECHANGE)
{
qDebug() << "winEvent in MgFrame : WM_DEVICECHANGE" ;
PDEV_BROADCAST_HDR lpdb = (PDEV_BROADCAST_HDR)msg->lParam;
switch(msg->wParam)
{
case DBT_DEVICEARRIVAL:
if (lpdb -> dbch_devicetype == DBT_DEVTYP_VOLUME)
{
PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)lpdb;
if (lpdbv -> dbcv_flags & DBTF_MEDIA)
{
char ALET = FirstDriveFromMask(lpdbv->dbcv_unitmask);
emit CD_Arrived
(QChar(ALET
));
qDebug() << "CD_Arrived ";
}
}
break;
case DBT_DEVICEREMOVECOMPLETE:
if (lpdb -> dbch_devicetype == DBT_DEVTYP_VOLUME)
{
PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)lpdb;
if (lpdbv -> dbcv_flags & DBTF_MEDIA)
{
char ALET = FirstDriveFromMask(lpdbv->dbcv_unitmask);
emit CD_Removed
(QChar(ALET
));
qDebug() << "CD_Removed";
}
}
break;
} // skip the rest
} // end of if msgType
return false; // let qt handle the rest
}
#include <QtCore>
#include <QtGui>
#include <QtDebug>
#include "mframe.h"
#include <dbt.h>
static char FirstDriveFromMask (ULONG unitmask)
{
char i;
for (i = 0; i < 26; ++i)
{
if (unitmask & 0x1)
break;
unitmask = unitmask >> 1;
}
return (i + 'A');
}
bool MFrame::winEvent(MSG * msg, long * result)
{
int msgType = msg->message;
if(msgType == WM_DEVICECHANGE)
{
qDebug() << "winEvent in MgFrame : WM_DEVICECHANGE" ;
PDEV_BROADCAST_HDR lpdb = (PDEV_BROADCAST_HDR)msg->lParam;
switch(msg->wParam)
{
case DBT_DEVICEARRIVAL:
if (lpdb -> dbch_devicetype == DBT_DEVTYP_VOLUME)
{
PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)lpdb;
if (lpdbv -> dbcv_flags & DBTF_MEDIA)
{
char ALET = FirstDriveFromMask(lpdbv->dbcv_unitmask);
emit CD_Arrived(QChar(ALET));
qDebug() << "CD_Arrived ";
}
}
break;
case DBT_DEVICEREMOVECOMPLETE:
if (lpdb -> dbch_devicetype == DBT_DEVTYP_VOLUME)
{
PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)lpdb;
if (lpdbv -> dbcv_flags & DBTF_MEDIA)
{
char ALET = FirstDriveFromMask(lpdbv->dbcv_unitmask);
emit CD_Removed(QChar(ALET));
qDebug() << "CD_Removed";
}
}
break;
} // skip the rest
} // end of if msgType
return false; // let qt handle the rest
}
To copy to clipboard, switch view to plain text mode
I found that It only works on the main window. This is why I emit CD_Arrived and
CD_Removed signals.
Hope this helps
Murielle
Bookmarks