转自http://www.qtcn.org/bbs/read-htm-tid-14535.html
在pro文件中应该加入
QT +=dbus
#include <QtDBus/QDBusConnection>
//以下为检测设备的插入
QDBusConnection::systemBus().connect( "org.freedesktop.Hal",
"/org/freedesktop/Hal/Manager",
"org.freedesktop.Hal.Manager",
"DeviceAdded",
this,
SLOT(slotDeviceAdded(QString )));
//以下为检查设备的拨出
QDBusConnection::systemBus().connect( "org.freedesktop.Hal",
"/org/freedesktop/Hal/Manager",
"org.freedesktop.Hal.Manager",
"DeviceRemoved",
this,
SLOT(slotDeviceRemoved(QString )));
在slotDeviceAdded(QString udi)函数中,要使用到
QDBusInterface device("org.freedesktop.Hal", udi, "org.freedesktop.Hal.Device" , QDBusConnection::systemBus());
通过HAL可以查询到设备为volume的设备,然后通过判断是否为/dev/sd*的设备,就可以判断出是否为U盘,然后调用mount就可以了。
这时记录下U盘的UDI,在检测到设备拨出时,再查询一下U盘的UDI是否还在,就知道U盘是否被拨出了。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)