1

我正在尝试通过以下方法检测 USB 设备是否已从 qt 程序中插入或移除:http: //doc.qt.io/qt-4.8/qdbusconnection.html#connect

我当前对应类的代码如下所示:

#include "usbhandler.h"
#include <QDebug>

USBHandler::USBHandler()
{
    QDBusConnection *bus;
    bool success;

    bus = new QDBusConnection("DeviceAdded");

    if (bus == NULL)
    {
        qDebug() << "Allocation Error";
        return;
    }

    if (!QDBusConnection::systemBus().isConnected())
    {
        qDebug() << "Cannot connect to system bus";
    }

    success = QDBusConnection::systemBus().connect(
                "org.freedesktop.UDisks",
                "/org/freedesktop/UDisks",
                "org.freedesktop.UDisks",
                "DeviceAdded",
            this, SLOT(deviceAdded(QDBusObjectPath)));

    if (success != true)
    {
        qDebug() << "Unsuccesfully connected!";
        delete bus;
        return;
    }
}

void USBHandler::deviceAdded(QDBusObjectPath dev)
{
  qDebug() << "device added!"<<dev.path();
}

当我跑步solid-hardware listen时,我在插入/拔出时收到以下通知:

Device Added:
udi = '/org/freedesktop/UDisks2/block_devices/sdb'
"/org/freedesktop/UDisks2/block_devices/sdb1" has new interfaces:     ("org.freedesktop.UDisks2.Block", "org.freedesktop.UDisks2.Filesystem", "org.freedesktop.UDisks2.Partition") 

Device Added:
udi = '/org/freedesktop/UDisks2/block_devices/sdb1'
"/org/freedesktop/UDisks2/block_devices/sdb1" lost interfaces: ("org.freedesktop.UDisks2.Partition", "org.freedesktop.UDisks2.Filesystem", "org.freedesktop.UDisks2.Block") 

Device Removed:
udi = '/org/freedesktop/UDisks2/block_devices/sdb1'
"/org/freedesktop/UDisks2/block_devices/sdb" lost interfaces: ("org.freedesktop.UDisks2.PartitionTable", "org.freedesktop.UDisks2.Block") 

Device Removed:
udi = '/org/freedesktop/UDisks2/block_devices/sdb'

所以我确信系统会通知 USB 记忆棒而且我现在已经使用 USB 命名接口玩了几个小时,但我的应用程序对插入/拔出记忆棒做出反应无法获得任何成功。

那么我做错了什么?接口和名称参数应该是什么connect()样的?你能解释一下他们到底是做什么的吗?

4

1 回答 1

-1
Device Added:
udi = '/org/freedesktop/UDisks2/block_devices/sdb'
"/org/freedesktop/UDisks2/block_devices/sdb1" has new interfaces:

我不确定这是你的问题,但udi包含UDisks2,而不是UDisks。我建议您将呼叫调整为connect(). 像这样的东西:

success = QDBusConnection::systemBus().connect(
            "org.freedesktop.UDisks",
            "/org/freedesktop/UDisks2",
            "org.freedesktop.UDisks2",
            [...]);
于 2015-08-28T09:05:26.110 回答