上来直接贴代码
完整工程文件下载
QT5 USB 插拔检测完整工程
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <windows.h>
#include <QMainWindow>
#include <dbt.h>
#include<devguid.h>
#include<SetupAPI.h>
#include<InitGuid.h>
#include <QString>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
char FirstDriveFromMask (ULONG unitmask);
protected:
bool nativeEvent(const QByteArray & eventType, void * message, long*result);
private slots:
void on_pushButton_clicked();
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <locale>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
#if 1
static

本文介绍如何在QT5环境下检测USB设备的插拔。通过分享一个完整的工程文件,包括mainwindow.h和mainwindow.cpp代码,展示了从VC工程移植过来的实现方式。在QT5中,需要重写QMainWindow的方法来处理USB插拔事件,但可能无法识别所有类型的USB设备,如usb-key。文章还提到了RegisterDeviceNotification函数的使用,以帮助识别更多设备。
最低0.47元/天 解锁文章
745





