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存储设备何时连接?


基础教程推荐
- C++,'if' 表达式中的变量声明 2021-01-01
- 设计字符串本地化的最佳方法 2022-01-01
- 运算符重载的基本规则和习语是什么? 2022-10-31
- 调用std::Package_TASK::Get_Future()时可能出现争用情况 2022-12-17
- C++ 程序在执行 std::string 分配时总是崩溃 2022-01-01
- 如何定义双括号/双迭代器运算符,类似于向量的向量? 2022-01-01
- 什么是T&&(双与号)在 C++11 中是什么意思? 2022-11-04
- C++ 标准:取消引用 NULL 指针以获取引用? 2021-01-01
- 您如何将 CreateThread 用于属于类成员的函数? 2021-01-01
- 如何在 C++ 中处理或避免堆栈溢出 2022-01-01