【发布时间】:2016-07-22 14:14:35
【问题描述】:
我希望在连接或断开新 USB 设备时收到通知(设备隐藏)。当有 USB 设备更改时,我已成功收到通知,但我不知道设备是连接还是断开连接。 我收到的消息(连接或断开 USB 时)是相同的: 消息:537(VM_DEVICECHANGE) w参数:7 lParam : 0
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QAbstractEventDispatcher>
#include <QAbstractNativeEventFilter>
#include <QDebug>
#include <windows.h>
#include <dbt.h>
#include <QObject>
class MyNativeEventFilter : public QAbstractNativeEventFilter {
public :
virtual bool nativeEventFilter( const QByteArray &eventType, void *message, long *result )
Q_DECL_OVERRIDE
{
if (eventType == "windows_generic_MSG")
{
MSG *msg = static_cast<MSG *>(message);
static int i = 0;
msg = (MSG*)message;
qDebug() << "message: " << msg->message << " wParam: " << msg->wParam
<< " lParam: " << msg->lParam;
if (msg->message == WM_DEVICECHANGE)
{
qDebug() << "WM_DEVICECHANGE";
}
}
return false;
}
};
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
MyNativeEventFilter myEventfilter;
app.eventDispatcher()->installNativeEventFilter(&myEventfilter);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}
【问题讨论】:
-
Offtopic:为什么需要自定义窗口?只需使用
qApp->eventDispatcher()->installNativeEventFilter( yourEventFilter);。yourEventFilter应该是QAbstractNativeEventFilter的孩子 -
@DmitrySazonov 谢谢你的技巧,但我总是遇到同样的“问题”(我已经编辑了我的帖子)
标签: windows qt events notifications usb