Getting/Sending a dictionary through dbus with Qt

A little about DBus

DBus is message bus system, where applications can exchange information. This information can be, numbers, data structures, called function or just simple messages. By default there are two standard buses: session bus and system bus.

Session bus: It is generally used by desktop applications.

System bus: It used to the traffic of messages from the operating system kernel.

In this post, I’ve tried to explain how to call a remote function that returns a dictionary structure.

Scheme with applications and bus session.

Scheme with applications and bus session.

Connecting dbus session

To connect to DBus system, is needed to inform the system bus and the bus name. Bus name is name of the available service.

It is something like org.mycompany.service.

conn = QDBusConnection(QDBusConnection::connectToBus(QDBusConnection::SessionBus, DBUS_BUS_NAME));

Getting the interface

After connect to the bus, it must obtein the interface object, to send/receive message trough bus. To achieve this goal, the bus name, path object and interface name must be informed.

QDBusInterface iface(DBUS_BUS_NAME, DBUS_OBJECT_PATH, DBUS_INTERFACE, conn); p, li { white-space: pre-wrap; }

Getting the dictionary

Invoque the remote method GetOptions returning a dictionary to reply variable.

QDBusReply<QVariantMap> reply = iface.call(“GetOptions”);

Setting the dictionary

Invoke the remote method SetOptions, passing a dictionary as argument.

iface.call(“SetOptions”, protocolOptions);

Leave a Reply