【问题标题】:QT DBUS mount filesystemQT DBUS挂载文件系统
【发布时间】:2012-09-09 01:09:21
【问题描述】:

我想使用 QT 和 DBUS 安装文件系统。我使用这个小 sn-p 订阅了“DeviceAdded”信号:

 void DBusWatcher::deviceAdded(const QDBusObjectPath &o) {
    QDBusMessage call = QDBusMessage::createMethodCall("org.freedesktop.UDisks", o.path(), "org.freedesktop.DBus.Properties", "GetAll");

    QList<QVariant> args;
    args.append("org.freedesktop.UDisks.Device");
    call.setArguments(args);

    QDBusPendingReply<QVariantMap> reply = DBusConnection::systemBus().asyncCall(call);
    reply.waitForFinished();

    QVariantMap map = reply.value();

    // ...
}

效果很好。我的问题是,我如何安装这个东西?我所拥有的就是这样的东西——它根本不起作用——而且没有错误。

QDBusMessage call = QDBusMessage::createMethodCall("org.freedesktop.UDisks", "dont know what to put here!", "org.freedesktop.UDisks.Device", "FilesystemMount");

现在,我应该在 QDBusConnection::systemBus() 上使用什么操作:call、asyncCall、callWithCallback?必须将什么作为第二个参数放入 createMethodCall?没有任何作用!真令人沮丧!

【问题讨论】:

    标签: c++ qt filesystems mount dbus


    【解决方案1】:

    好的,经过至少2天的努力,我终于搞定了!我查看了razer-qt 来源,我查看了kdelibs 来源,但不知何故他们所有的dbus 东西都不起作用。所以这是我非常满意的 sn-p:

    void DBusWatcher::deviceAdded(const QDBusObjectPath &o) {
        QDBusMessage call = QDBusMessage::createMethodCall("org.freedesktop.UDisks", o.path(), "org.freedesktop.DBus.Properties", "GetAll");
    
        QList<QVariant> args;
        args.append("org.freedesktop.UDisks.Device");
        call.setArguments(args);
    
        QDBusPendingReply<QVariantMap> reply = QDBusConnection::systemBus().asyncCall(call);
        reply.waitForFinished();
    
        QVariantMap map = reply.value();
        // now do what you want with the map ;)
        // You will find all available information to the device attached
    }
    
    // a class wide pointer to the systembus
    // initialized within the constructor of the class
    // and deleted in the destructor
    dbus = new QDBusInterface(
        "org.freedesktop.UDisks",
        "here comes the path from the QDBusObjectPath.path() object",
        "org.freedesktop.UDisks.Device",
        QDBusConnection::systemBus(),
        this
    );
    
    void DbusAction::mountFilesystem() {
        if(dbus->isValid()) {
    
            QList<QVariant> args;
            args << QVariant(QString()) << QVariant(QStringList());
    
            QDBusMessage msg = dbus->callWithArgumentList(QDBus::AutoDetect, "FilesystemMount", args);
            if(msg.type() == QDBusMessage::ReplyMessage) {
                QString path = msg.arguments().at(0).toString();
                if(!path.isEmpty()) {
                    emit deviceMounted(path);
                } else {
                    qDebug() << "sorry, but the path returned is empty";
                }
            } else {
                qDebug() << msg.errorMessage();
            }
        }
    }
    

    我正在使用在 x64-ArchLinux 上运行的 Openbox 和最新的 Udisk(2) 东西。也许有人也可以使用它。

    【讨论】:

      猜你喜欢
      • 2015-03-03
      • 1970-01-01
      • 1970-01-01
      • 2011-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-04
      相关资源
      最近更新 更多