Qt如何知道新的USB存储设备何时连接?

How to know when a new USB storage device is connected in Qt?(Qt如何知道新的USB存储设备何时连接?)
本文介绍了Qt如何知道新的USB存储设备何时连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我想知道 USB 设备何时连接到运行我的 Qt 应用程序的计算机(在 Windows 中).在我的主要 QWidget 中,我重新实现了 winEventFilter,如下所示:

I want to know when a USB device is connected to the computer that my Qt application is running on (in Windows). In my main QWidget, I've reimplemented winEventFilter like this:

bool winEventFilter ( MSG * msg, long * result ) {
    qDebug() << msg;
    return false;
}

当我连接 USB 设备时,我希望 qDebug 至少发送一些东西,但我什么也没收到.

I'd expect qDebug to send at least something when I connect a USB device, but I don't get anything.

我猜我从根本上误解了这里的过程 - 这是我的第一个 Qt 应用程序!

I'm guessing that I'm fundamentally misunderstanding the process here - this is my first Qt app!

推荐答案

我相信您可能缺少的是注册设备通知的调用.这是我用来做同样事情的代码,虽然我覆盖了 QWidget 类的 winEvent() 方法而不是 winEventFilter.

I believe what you may be missing is the call to register for device notification. Here is code that I use to do the same thing, though I override the winEvent() method of the QWidget class and not the winEventFilter.

// Register for device connect notification
DEV_BROADCAST_DEVICEINTERFACE devInt;
ZeroMemory( &devInt, sizeof(devInt) );
devInt.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
devInt.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
devInt.dbcc_classguid = GUID_DEVINTERFACE_VOLUME;

m_hDeviceNotify =
    RegisterDeviceNotification( winId(), &devInt, DEVICE_NOTIFY_WINDOW_HANDLE );   
if(m_hDeviceNotify == NULL)
{
    qDebug() << "Failed to register device notification";
} // end if

注意:您很可能需要更改 DEV_BROADCAST_DEVICEINTERFACE 的值以满足您的需要.

NOTE: You will most likely need to change the values of the DEV_BROADCAST_DEVICEINTERFACE to fit your needs.

要使用此代码,您需要包含正确的头文件并执行正确的设置.DEV_BROADCAST_DEVICEINTERFACE 需要包含 Dbt.h 标头.此外,此代码的重点是 RegisterDeviceNotification 函数.信息可在 MSDN

To use this code you will need to include the proper header files and perform the proper setup. DEV_BROADCAST_DEVICEINTERFACE requires the Dbt.h header to be included. Also, the focal point of this code is on the RegisterDeviceNotification function. Info is available on MSDN

这篇关于Qt如何知道新的USB存储设备何时连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Bring window to front -gt; raise(),show(),activateWindow() don’t work(把窗户放在前面 -raise(),show(),activateWindow() 不起作用)
How to get a list video capture devices NAMES (web cameras) using Qt (crossplatform)? (C++)(如何使用 Qt(跨平台)获取列表视频捕获设备名称(网络摄像机)?(C++))
How to compile Qt as static(如何将 Qt 编译为静态)
C++ over Qt : Controlling transparency of Labels and Buttons(C++ over Qt:控制标签和按钮的透明度)
What is an event loop in Qt?(Qt 中的事件循环是什么?)
How to implement a video widget in Qt that builds upon GStreamer?(如何在基于 GStreamer 的 Qt 中实现视频小部件?)