Qt U盘拔插监控 源码分享

本文介绍了一款跨平台程序,利用Qt4/5和QDeviceWatcher库实时监控USB插入和移除事件。程序可在Windows和Ubuntu上运行,通过下载源码、编译静态库并集成到项目中,可以方便地检测U盘的插入、移除和状态变化。此外,还提供了使用QDBus在Ubuntu环境下实现U盘监控的替代方案。
摘要由CSDN通过智能技术生成

前言

程序兼容windows/ubuntu平台,支持Qt4和Qt5版本

源码下载

大佬WangBin的源码下载地址 https://github.com/wang-bin/qdevicewatcher

程序运行效果

程序功能:实时监控USB插入和弹出事件
U盘插入效果在这里插入图片描述
U盘弹出效果在这里插入图片描述

使用方法

1.使用QtCreator打开文件QDeviceWatcher.pro并直接编译
2.编译完成后,构造目录下将生成静态库
windows环境下生成在lib_win_文件夹内。
在这里插入图片描述
ubuntu环境下生成在lib_linux_文件夹内。在这里插入图片描述
3.在你的项目中使用刚生成的静态库
3.1将你刚才的变成生成的静态库文件和库所需要用到的头文件放到你的项目中。windows环境下所需文件有qdevicewatcher.h、qdevicewatcher_p.h、libQDeviceWatcherd2.a
3.2在pro文件中添加库
在这里插入图片描述
3.3 添加hotplugwatcher.h头文件

/******************************************************************************
    Name: description
    Copyright (C) 2011-2015 Wang Bin <wbsecg1@gmail.com>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
******************************************************************************/

#ifndef HOTPLUGWATCHER_H
#define HOTPLUGWATCHER_H

#include "qdevicewatcher.h"
#include <QtCore/QObject>
#include <QtCore/QThread>

#ifndef __GNUC__
#define __PRETTY_FUNCTION__ __FUNCTION__
#endif

class HotplugWatcher : public QThread
{
    Q_OBJECT
public:
    HotplugWatcher(QObject *parent = 0)
        : QThread(parent)
    {
        qDebug("tid=%#x %s", (quintptr) QThread::currentThreadId(), __PRETTY_FUNCTION__);
        start();

        moveToThread(this); //Let bool event(QEvent *e) be in another thread
        watcher = new QDeviceWatcher;
        watcher->moveToThread(this);
        watcher->appendEventReceiver(this);
        connect(watcher,
                SIGNAL(deviceAdded(QString)),
                this,
                SLOT(slotDeviceAdded(QString)),
                Qt::DirectConnection);
        connect(watcher,
                SIGNAL(deviceChanged(QString)),
                this,
                SLOT(slotDeviceChanged(QString)),
                Qt::DirectConnection);
        connect(watcher,
                SIGNAL(deviceRemoved(QString)),
                this,
                SLOT(slotDeviceRemoved(QString)),
                Qt::DirectConnection);
        watcher->start();
    }

public slots:
    void slotDeviceAdded(const QString &dev)
    {
        emit signal_addUsb(dev);
        qDebug("tid=%#x %s: add %s",
               (quintptr) QThread::currentThreadId(),
               __PRETTY_FUNCTION__,
               qPrintable(dev));
    }
    void slotDeviceRemoved(const QString &dev)
    {
        emit signal_removeUsb(dev);
        qDebug("tid=%#x %s: remove %s",
               (quintptr) QThread::currentThreadId(),
               __PRETTY_FUNCTION__,
               qPrintable(dev));
    }
    void slotDeviceChanged(const QString &dev)
    {
        emit signal_changeUsb(dev);
        qDebug("tid=%#x %s: change %s",
               (quintptr) QThread::currentThreadId(),
               __PRETTY_FUNCTION__,
               qPrintable(dev));
    }
signals:
    void signal_addUsb(QString);
    void signal_removeUsb(QString);
    void signal_changeUsb(QString);
protected:
    virtual bool event(QEvent *e)
    {
        if (e->type() == QDeviceChangeEvent::registeredType()) {
            QDeviceChangeEvent *event = (QDeviceChangeEvent *) e;
            QString action("Change");
            if (event->action() == QDeviceChangeEvent::Add)
                action = "Add";
            else if (event->action() == QDeviceChangeEvent::Remove)
                action = "Remove";

            qDebug("tid=%#x event=%d %s: %s %s",
                   (quintptr) QThread::currentThreadId(),
                   e->type(),
                   __PRETTY_FUNCTION__,
                   qPrintable(action),
                   qPrintable(event->device()));
            event->accept();
            return true;
        }
        return QObject::event(e);
    }
private:
    QDeviceWatcher *watcher;
};

#endif // HOTPLUGWATCHER_H

3.4 创建connect信号槽监视U盘拔插情况

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    watcher = new HotplugWatcher();
    qDebug("Hotplug watcher(libQDeviceWatcher test app)\nwbsecg1@gmail.com\n");
    connect(watcher,&HotplugWatcher::signal_addUsb,this,&MainWindow::onAddUsb);
    connect(watcher,&HotplugWatcher::signal_removeUsb,this,&MainWindow::onRemoveUsb);
    connect(watcher,&HotplugWatcher::signal_changeUsb,this,&MainWindow::onChangeUsb);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::onAddUsb(QString dev)
{
    qDebug() << "onAddUsb" << dev;
}

void MainWindow::onRemoveUsb(QString dev)
{
    qDebug() << "onRemoveUsb" << dev;
}

void MainWindow::onChangeUsb(QString dev)
{
    qDebug() << "onChangeUsb" << dev;
}

QDBus方式实现

如果你是Ubuntu环境,而且觉得上面说的方式移植很麻烦,不妨使用QDBus的方式区实现U盘拔插监控。建议参考文章:Qt QDBus实现的磁盘管理器

  • 3
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jbyyy、

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值