Detecting USB notification in Qt on windows

后端 未结 2 1716
天涯浪人
天涯浪人 2021-01-19 17:53

In my qt application I want to save some application output data to an file in my usb pen drive. I need to put following features in my qt application

  1. Detect t
2条回答
  •  长发绾君心
    2021-01-19 18:15

    The earlier answer is now out-of-date. The following worked for me with QT5 on Windows 10, where MainWindow is derived from QMainWindow:

    #include 
    #include 
    #include 
    
    bool MainWindow::nativeEvent(const QByteArray& eventType, void* pMessage, long* pResult)
    {
        auto pWindowsMessage = static_cast(pMessage);
        auto wParam = pWindowsMessage->wParam;
        if (wParam == DBT_DEVICEARRIVAL || wParam == DBT_DEVICEREMOVECOMPLETE) {
            auto lParam = pWindowsMessage->lParam;
            auto deviceType = reinterpret_cast(lParam)->dbch_devicetype;
            if (deviceType == DBT_DEVTYP_VOLUME) {
                auto unitmask = reinterpret_cast(lParam)->dbcv_unitmask;
                for (int i = 0; i < 32; ++i) {
                    if ((unitmask & (1 << i)) != 0) {
                        setDriveChanged('A' + i, wParam == DBT_DEVICEARRIVAL);
                    }
                }
            }
        }
        return false;
    }
    

提交回复
热议问题