問題描述
我想知道 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存儲設備何時連接?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!