Development/Tutorials/D-Bus/Accessing Interfaces: Difference between revisions

From KDE TechBase
(The page was moved again)
 
(68 intermediate revisions by 18 users not shown)
Line 1: Line 1:
== Abstract ==
This tutorial was updated and moved to https://develop.kde.org/docs/features/d-bus/accessing_dbus_interfaces/


D-Bus allows applications to expose internal API to the outside world. These APIs can then be accessed at run-time via the D-Bus protocol using command line applications or D-Bus libraries and bindings themselves. This tutorial looks at the latter method with examples that you can use in your applications.
[[Category:MovedDevelop]]
 
== Prerequisites ==
Technologies:
* Qt4
* D-Bus
 
 
Tutorials:
* [[Development/Tutorials/Introduction_To_D-Bus|Introduction To D-Bus]]
 
== D-Bus Interfaces ==
 
 
== Using QDBusMessage ==
 
{{qt|QDBusMessage}} represents a D-BUS messages that can be sent or has been received over a given bus. It can be used directly to call methods in D-BUS services using the <tt>{{qt|QDbusMessage}}::createMethod( const QString & service, const QString & path, const QString & interface, const QString & method )</tt> static method. It returns a {{qt|QDBusMessage}} object that you can then use to make the call.
 
The <tt>interface</tt> parameter is optional and only necessary if the method to be called is not unique in the accessed object address by the <tt>path</tt>. This can happen with the object implements multiple interfaces and those interfaces have methods with the same name. In such (rare) cases, if you do not  define the interface to use there is not guarantee as to which method will actually get called. However, usually you can simply pass an empty string (e.g. <tt>""</tt>) as the argument for <tt>interface</tt>.
 
 
By way of example, to access the (fictional) <tt>ping</tt> method on the {{path|/network}} object in the <tt>org.foo.bar</tt> service, one might do this:
 
<code cppqt n>QDBusMessage* m = QDBusMessage::createMessage("org.foo.bar",
                                            "/network",
                                            "",
                                            "ping");
bool queued = QDBusConnection::sessionBus()->send(m);</code>
 
In line 5 of the above example we queue the message for sending on the current session bus. We get a <tt>bool</tt> returned letting us know if the queuing was successful or not.
 
This leaves us with two questions, however:
* How can one set parameters for a method call?
* How can one get a return message in the case of D-Bus methods that have a return value?
 
=== Setting Parameters ===
 
Sending arguments along with the method call is quite straight forward. First we need to create a {{qt|QList}} of {{qt|QVariant}} objects and then add those to our dbus message. So if the <tt>ping</tt> method in the above took a hostname as a parameter, we might alter the code in this way:
 
<code cppqt n>QDBusMessage* m = QDBusMessage::createMessage("org.foo.bar",
                                            "/network",
                                            "",
                                            "ping");
<b>QList<QVariant> args;
args.append("kde.org");
m->setArguments(args);</b>
bool queued = QDBusConnection::sessionBus()->send(m);</code>
 
{{note|The arguments must appear in the {{qt|QList}} in the same order they are expected by the D-Bus method being called.}}
 
== Using QDBusInterface ==
 
== Using An Interface ==
 
== Connecting To Signals ==
 
== Other Resources ==

Latest revision as of 20:58, 18 July 2023