Debian Codegraph @

src/dbus/doc/snippets/code/src_qdbus_qdbuspendingcall.cpp

raw
1 // Copyright (C) 2016 The Qt Company Ltd.
2 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3 #include <QDBusPendingCall>
4 #include <QDBusInterface>
5 #include <QDBusPendingReply>
6
7 class DBus_PendingCall_Interface : public QObject
8 {
9 Q_OBJECT
10
11 public:
12 DBus_PendingCall_Interface(QObject *parent = nullptr)
13 : QObject(parent) {
14 iface = new QDBusInterface("org.example.Interface", "/Example/Methods");
15 }
16
17 ~DBus_PendingCall_Interface() { delete iface; }
18 void callInterfaceMain();
19 void showError();
20 void showReply(QString&, QByteArray&);
21 QString value1;
22 QString value2;
23 void callFinishedSlot(QDBusPendingCallWatcher *call);
24 public slots:
25
26 private:
27 QDBusInterface *iface;
28 };
29
30 void DBus_PendingCall_Interface::callInterfaceMain()
31 {
32 //! [0]
33 QDBusPendingCall async = iface->asyncCall("RemoteMethod", value1, value2);
34 QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(async, this);
35
36 QObject::connect(watcher, &QDBusPendingCallWatcher::finished, this,
37 &DBus_PendingCall_Interface::callFinishedSlot);
38 //! [0]
39
40 }
41
42 //! [1]
43 void DBus_PendingCall_Interface::callFinishedSlot(QDBusPendingCallWatcher *call)
44 {
45 QDBusPendingReply<QString, QByteArray> reply = *call;
46 if (reply.isError()) {
47 showError();
48 } else {
49 QString text = reply.argumentAt<0>();
50 QByteArray data = reply.argumentAt<1>();
51 showReply(text, data);
52 }
53 call->deleteLater();
54 }
55 //! [1]