DBus and Qt programming

Posted by in Software

For the past few days I’ve been trying to get more complex data sent via D-Bus. As one would say here in Ireland:

Je’zus H. Christ

Playing with python was easy. There are lots of examples on communicating complex data structures. But with Qt… It’s a bit more complex, because there are only a set of simple examples.

To keep things simple, I chose the car example:

  • The car/ is the server. It receives the request from the client and sends a reply.
  • The controller/ is the client. It “calls” a method on the server, gets the results and processes them

Notice the extra method called structer with two outputs:

  • a structure MyStruct
  • a qlonglong

For the purpose of this test, MyStruct is a simple structure:

The client calls structer. The server updates the qlonglong and the MyStruct object and sends them back to the client to be used.

The D-Bus translation

First, note the “arg0” annotation, required for the struct type:

This actually helps the qdbusxml2cpp to typ-ify the generated code.

On the client (controller) part, we use the following command:

On the server (car) part, use the command:

for the same car.xml file.

The XML resulted from the introspection description for structer is translated by the Qt into:

aka first “out” argument is passed as the return argument for the function. The other arguments are passes as function arguments. It’s good to know this, because you can implement your own method without looking at the *_p.h files for guidance.

… but not yet.

The Server Side

First issue is that qdbusxml2cpp is not smart enough to implement custom marshaling. It offers instead a guideline for us:

once we change the commented line to something like:

we’re good to go. The line

MyStruct s = static_cast<car *>(parent())->structer(out1);

implies that the parent() object is a pointer to Car, which, in turn contains a structer() implementation. You can change the call into whatever code capable of updating the out1 parameter and returning a MyStruct struct.

For the purpose of this exercise, we implement a structer() method in our car.cpp like so:

and include the car.h in.

The downfall of this server approach is that it requires manual adjustment of a generated file. So, once you have the .cpp and the _p.h files available, save them to your repository. Have them regenerated only if needed. Note that Trolltech’s Qt tarball contains already generated files, instead of scripting :)

The Client Side

Here the fun begins! First, open the .ui file and add a new button to call the structer(). In controller.h complete the on_XXXX_clicked() with the new button ID (let’s call it on_structer clicked())

The implementation follows the D-Bus guidelines from the other buttons:

The result is that every time you press the button, it’s going to call the server (car) and get the MyStruct structure from the server.

… but not yet! :)

The Glue

D-Bus is a relatively simple mechanism and supports a predefined list of types. Composite types are sent as structures and have to have custom marshaling implemented. This is done via <</>> C++ operators.

Once you have the structure defined, the Qt way is a three-step process.

Declare the Metatype

Place the line

somewhere outside body/class/namespace blocks. I’ve put it in mystruct.h after the struct declaration.

Declare/Implement the Marshaller

This actually means you need to declare/implement the <</>> operators. For convenience, I’ve declared them in mystruct.h as well and implemented them in mystruct.cpp like this:

mystruct.h

mystruct.cpp

Register the Type with D-Bus

Once you have done the above steps, everything is almost ready. Now you only need to add the type to the list of custom marshallers with qDBusRegisterMetaType.

In mystruct.h, add:

and have it called in both the client and server, somewhere before the actual D-Bus methods are called (e.g. in main() or in some constructor).

Look at the example for details.

Good luck at hacking your own D-Bus stuff!


A little experiment: If you find this post and ad below useful, please check the ad out :-)