Debian Codegraph @

src/dbus/doc/snippets/code/src_qdbus_qdbuspendingreply.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_PendingReply_Interface : public QObject
8 {
9 Q_OBJECT
10
11 public:
12 DBus_PendingReply_Interface(QObject *parent = nullptr)
13 : QObject(parent) {
14 iface = new QDBusInterface("org.example.Interface", "/Example/Methods");
15 }
16
17 ~DBus_PendingReply_Interface() { delete iface; }
18 void callInterfaceMainR();
19 void PendingReplyString();
20 void PendingReplyBool();
21 void showErrorD(QDBusError);
22 void showSuccess(QVariant);
23 void showFailure(QVariant);
24 void useValue(QDBusPendingReplyTypes::Select<0, QString, void, void, void, void, void, void, void>::Type);
25 public slots:
26
27 private:
28 QDBusInterface *iface;
29 };
30
31 void DBus_PendingReply_Interface::PendingReplyString()
32 {
33 //! [0]
34 QDBusPendingReply<QString> reply = iface->asyncCall("RemoteMethod");
35 reply.waitForFinished();
36 if (reply.isError())
37 // call failed. Show an error condition.
38 showErrorD(reply.error());
39 else
40 // use the returned value
41 useValue(reply.value());
42 //! [0]
43 }
44
45 void DBus_PendingReply_Interface::PendingReplyBool()
46 {
47 //! [2]
48 QDBusPendingReply<bool, QString> reply = iface->asyncCall("RemoteMethod");
49 reply.waitForFinished();
50 if (!reply.isError()) {
51 if (reply.argumentAt<0>())
52 showSuccess(reply.argumentAt<1>());
53 else
54 showFailure(reply.argumentAt<1>());
55 }
56 //! [2]
57 }