summaryrefslogtreecommitdiffstats
path: root/src/dbus/doc/snippets/code/src_qdbus_qdbuspendingcall.cpp
blob: 67b019a67d007953509ae47c14746ff323deb37c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#include <QDBusPendingCall>
#include <QDBusInterface>
#include <QDBusPendingReply>

class DBus_PendingCall_Interface : public QObject
{
    Q_OBJECT

public:
    DBus_PendingCall_Interface(QObject *parent = nullptr)
    : QObject(parent) {
        iface = new QDBusInterface("org.example.Interface", "/Example/Methods");
    }

    ~DBus_PendingCall_Interface() {  delete iface; }
    void callInterfaceMain();
    void showError();
    void showReply(QString&, QByteArray&);
    QString value1;
    QString value2;
    void callFinishedSlot(QDBusPendingCallWatcher *call);
public slots:

private:
    QDBusInterface *iface;
};

void DBus_PendingCall_Interface::callInterfaceMain()
{
//! [0]
    QDBusPendingCall async = iface->asyncCall("RemoteMethod", value1, value2);
    QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(async, this);

    QObject::connect(watcher, &QDBusPendingCallWatcher::finished, this,
                     &DBus_PendingCall_Interface::callFinishedSlot);
//! [0]

}

//! [1]
void DBus_PendingCall_Interface::callFinishedSlot(QDBusPendingCallWatcher *call)
{
    QDBusPendingReply<QString, QByteArray> reply = *call;
    if (reply.isError()) {
        showError();
    } else {
        QString text = reply.argumentAt<0>();
        QByteArray data = reply.argumentAt<1>();
        showReply(text, data);
    }
    call->deleteLater();
}
//! [1]