如何知道什么时候一个新的USB存储设备连接在Qt? [英] How to know when a new USB storage device is connected in Qt?

查看:619
本文介绍了如何知道什么时候一个新的USB存储设备连接在Qt?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道何时将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;
}



我希望qDebug在连接USB设备时至少发送一些东西,但我没有得到任何东西。

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆