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

时间:2023-01-23
本文介绍了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 中的事件循环是什么? 下一篇:C++ over Qt:控制标签和按钮的透明度

相关文章

最新文章