• Qt在Windows系统下检索U盘的插拔


    1、检索U盘

    //检索是否插入U盘U盘
    void MainWindow::onSigDeviceChange(MSG *msg)
    {
        mVectorDiskVolume.clear();
        PDEV_BROADCAST_HDR lpdb = (PDEV_BROADCAST_HDR)msg->lParam;
        switch(msg->wParam)
        {
        case DBT_DEVICETYPESPECIFIC:
    
            break;
        case DBT_DEVICEARRIVAL://系统检测到新设备
    
            if(lpdb->dbch_devicetype == DBT_DEVTYP_VOLUME)
            {
    
                 PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)lpdb;
                 if(lpdbv->dbcv_flags ==0)
                 {
                     //插入u盘
                     QString USBDisk = QString(this->FirstDriveFromMask(lpdbv ->dbcv_unitmask));
                     mVectorDiskVolume.append(USBDisk);
                     setCheckBoxState();
                 }
            }
            break;
        case DBT_DEVICEREMOVECOMPLETE://设备已丢失
    
            if(lpdb->dbch_devicetype == DBT_DEVTYP_VOLUME)
            {
                PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)lpdb;
                if(lpdbv->dbcv_flags == 0)
                {
                    QString USBDisk = QString(this->FirstDriveFromMask(lpdbv ->dbcv_unitmask));
                    mVectorDiskVolume.removeAt(mVectorDiskVolume.indexOf(USBDisk));
                }
            }
            break;
        default:
            break;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    char MainWindow::FirstDriveFromMask(ULONG unitmask)
    {
        char i;
        for (i = 0; i < 26; ++i)
        {
            if (unitmask & 0x1)
                break;
            unitmask = unitmask >> 1;
        }
        return (i + 'A');
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    重写nativeEvent函数

    bool MainWindow::nativeEvent(const QByteArray &eventType, void *message, long *result)
    {
        //MSG是个消息结构体,里面有类型、数据等。
        MSG* msg = reinterpret_cast<MSG*>(message);
    
        if (msg->message == WM_DEVICECHANGE)
        {
            emit(sigDeviceChange(msg));
        }
    
        if (eventType == "windows_generic_MSG" || eventType == "windows_dispatcher_MSG") {
            MSG* msg = static_cast<MSG *>(message);
            switch (msg->message)
            {
            case WM_KEYDOWN:
            case WM_SYSKEYDOWN:
                {
                    if ((VK_F4 == msg->wParam) && (::GetKeyState(VK_MENU) & 0xF000))
                    {
    
                        return TRUE;
                    }
                }
                break;
            default:
                break;
            }
        }
    
        return QWidget::nativeEvent(eventType, message, result);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    关联信号槽

     connect(this, SIGNAL(sigDeviceChange(MSG*)), SLOT(onSigDeviceChange(MSG*)));
    
    • 1

    头文件

    #include 
    #include "dbt.h"
    
    • 1
    • 2
  • 相关阅读:
    AUTOSAR AP 硬核知识点梳理(2)— 架构详解
    Cesium 地球(2)-瓦片创建
    多模态大一统:通向全模态学习和通用人工智能的未来之路
    【FPGA】FPGA入门 —— 基本开发流程
    25个Matplotlib图的Python代码,复制直接可用
    【Linux】图文详解Xshell远程连接服务器:以Amazon EC2 VPS为例
    SAAJ:SOAP with Attachments API for Java interface
    STL相关面试问题
    Java实现异步编程的8种方式
    如何用Angular和NativeScript开发IOS程序?
  • 原文地址:https://blog.csdn.net/weixin_44585751/article/details/126836107