程序兼容windows/ubuntu平台,支持Qt4和Qt5版本
源码下载大佬WangBin的源码下载地址 https://github.com/wang-bin/qdevicewatcher
程序运行效果程序功能:实时监控USB插入和d出事件
U盘插入效果
U盘d出效果
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
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
#include
#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
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实现的磁盘管理器
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)