Client :: Method Call

方法1: 直接使用 Message 传递消息(Low-Level-API)

// 传参数
QDBusMessage msg = QDBusMessage::createMethodCall("com.myabc.service",
                             "/", "com.myabc.interface", "setName");
msg << QString("Bright");
QDBusMessage response = QDBusConnection::sessionBus().call(msg);
 
// 获取返回值
QDBusMessage msg = QDBusMessage::createMethodCall("com.myabc.service",
                             "/", "com.myabc.interface", "name");
QDBusMessage response = QDBusConnection::sessionBus().call(msg);
 
// 判断 Method 是否被正确返回
if(response.type() == QDBusMessage::ReplyMessage)
{
    // QDBusMessage的arguments不仅可以用来存储发送的参数,也用来存储返回值
    // 这里取得 checkIn 的返回值
    QString name= response.arguments().takeFirst().toString();
}

方法2: 通过 DBusInterface 调用方法(同步+异步)

QDBusInterface interface("com.myabc.service", "/",
                         "com.myabc.interface",
                         QDBusConnection::sessionBus());
if(!interface.isValid())
{
    qDebug() << qPrintable(QDBusConnection::sessionBus().lastError().message());
    exit(1);
}
 
// 调用远程对象的方法 setName()
interface.call("setName", "Bright");
 
// 调用 name() 并获取返回值
QDBusReply<QString> reply = interface.call("name");
if(reply.isValid())
{
    QString value = reply.value();
    qDebug() << "value = " << value ;
}

interface::asyncCall( ) 异步调用

QDBusPendingCall async = interface->asyncCall("setName", "Brion");
// or use this: QDBusPendingReply<QString> reply = interface->asyncCall("RemoteMethod");
async.waitForFinished ();
QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(async, this);
 
QObject::connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)),
                 this, SLOT(callFinishedSlot(QDBusPendingCallWatcher*)));
 
void MyClass::callFinishedSlot(QDBusPendingCallWatcher *call)
{
    QDBusPendingReply<QString> reply = *call;
    if (! reply.isError()) {
        QString name= reply.argumentAt<0>();
        qDebug() << "name = " << name;
    }
    call->deleteLater();
}

方法3: 从XML导入代理类

A. 使用工具qdbuscpp2xml从object.h生成XML文件:

qdbuscpp2xml -M test.h -o com.scorpio.test.xml

<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN" "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">;;
<node>
  <interface name="com.scorpio.test.value">
    <method name="maxValue">
      <arg type="i" direction="out"/>
    </method>
    <method name="minValue">
      <arg type="i" direction="out"/>
    </method>
    <method name="value">
      <arg type="i" direction="out"/>
    </method>
  </interface>
</node>

B. 使用工具qdbusxml2cpp从XML文件生成继承自QDBusInterface的类

qdbusxml2cpp com.scorpio.test.xml -p valueInterface

生成两个文件:valueInterface.cpp 和 valueInterface.h  &  valueInterface.h文件:
/*
 * This file was generated by qdbusxml2cpp version 0.7
 * Command line was: qdbusxml2cpp com.scorpio.test.xml -p testInterface
 *
 * qdbusxml2cpp is Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
 *
 * This is an auto-generated file.
 * Do not edit! All changes made to it will be lost.
 */

#ifndef TESTINTERFACE_H_1526737677
#define TESTINTERFACE_H_1526737677

#include <QtCore/QObject>
#include <QtCore/QByteArray>
#include <QtCore/QList>
#include <QtCore/QMap>
#include <QtCore/QString>
#include <QtCore/QStringList>
#include <QtCore/QVariant>
#include <QtDBus/QtDBus>

/*
 * Proxy class for interface com.scorpio.test.value
 */
class ComScorpioTestValueInterface: public QDBusAbstractInterface
{
    Q_OBJECT
public:
    static inline const char *staticInterfaceName()
    { return "com.scorpio.test.value"; }

public:
    ComScorpioTestValueInterface(const QString &service, const QString &path, const QDBusConnection &connection, QObject *parent = 0);

    ~ComScorpioTestValueInterface();

public Q_SLOTS: // METHODS
    inline QDBusPendingReply<int> maxValue()
    {
        QList<QVariant> argumentList;
        return asyncCallWithArgumentList(QLatin1String("maxValue"), argumentList);
    }

    inline QDBusPendingReply<int> minValue()
    {
        QList<QVariant> argumentList;
        return asyncCallWithArgumentList(QLatin1String("minValue"), argumentList);
    }

    inline QDBusPendingReply<int> value()
    {
        QList<QVariant> argumentList;
        return asyncCallWithArgumentList(QLatin1String("value"), argumentList);
    }

Q_SIGNALS: // SIGNALS
};

namespace com {
  namespace scorpio {
    namespace test {
      typedef ::ComScorpioTestValueInterface value;
    }
  }
}
#endif
View Code

相关文章:

  • 2022-01-20
  • 2021-05-31
  • 2022-01-22
  • 2021-10-25
  • 2021-08-02
  • 2021-11-25
  • 2021-09-28
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案