How to know when a new USB storage device is connected in Qt?(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存储设备何时连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Qt如何知道新的USB存储设备何时连接?
基础教程推荐
- 这个宏可以转换成函数吗? 2022-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 常量变量在标题中不起作用 2021-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
