Difference between revisions of "Development/Tutorials/D-Bus/Intermediate D-Bus"

Jump to: navigation, search
(Start collection of real world dbus tips and tricks)
 
(Arbitrary list return type)
Line 17: Line 17:
 
=== Dicts ===
 
=== Dicts ===
 
The DBus Dict type should map to QMap, example to follow..
 
The DBus Dict type should map to QMap, example to follow..
 +
 +
=== Arbitrary sets of return types ===
 +
Some D-Bus methods return an arbitrary tuple of values.  The {{qt|QDBusReply}} class can only handle the first value returned by a method, so to get the rest of the returned parameters we fall back to using {{qt|QDBusMessage}}.  Since QDBusAbstractInterface::call() and similar actually return QDBusMessage, when we use QDBusReply we are actually just constructing this from the QDBusMessage containing all the return values.
 +
 +
Once we have the {{qt|QDBusMessage}}, we can access the return values using arguments() which returns a QList<QVariant>.
 +
 +
For example, for a method
 +
<tt> org.kde.DBusTute.Favourites.Get( out INT32 number, out STRING colour, out STRING flavour )</tt>,
 +
we would use the following code:
 +
 +
 +
<code cppqt n>
 +
QDBusInterface iface( "org.kde.DBusTute",
 +
                      "/org/kde/DBusTute/Favourites",
 +
                      "org.kde.DBusTute.Favourites",
 +
                      QDBus::sessionBus(), 0 );
 +
QDBusMessage reply = iface.call( "Get" );
 +
QList<QVariant> values = return.arguments();
 +
int favouriteNumber = values.takeFirst().toInt();
 +
QString favouriteColour = values.takeFirst().toString();
 +
QString favouriteFlavour = values.takeFirst().toString();
 +
</code>

Revision as of 00:12, 11 March 2007

Contents

Abstract

The basic techniques explained in Accessing Interfaces are suitable for using D-Bus methods with relatively simple signatures, but the more complex interfaces often found in the wild require additional techniques to address, explained in this article.

Complex Return Types

QtDBus requires additional setup to deal with methods that return more complex return types than single primitives. The return type needs to be declared to the Qt type system so that it can be demarshalled.

Lists

Lists of values returned by D-Bus methods are mapped to QList in QtDBus. The appropriate specialisation of QList should be declared as a type to the Qt type system, for example:

Q_DECLARE_METATYPE(QList<QDBusObjectPath>)

It is essential that the Q_DECLARE_METATYPE macro is used outside any code blocks or methods in source code. The best place to use it is at the top of the file.

The type should also be declared to QtDbus using:

qDBusRegisterMetaType<QList<QDBusObjectPath> >();

Dicts

The DBus Dict type should map to QMap, example to follow..

Arbitrary sets of return types

Some D-Bus methods return an arbitrary tuple of values. The QDBusReply class can only handle the first value returned by a method, so to get the rest of the returned parameters we fall back to using QDBusMessage. Since QDBusAbstractInterface::call() and similar actually return QDBusMessage, when we use QDBusReply we are actually just constructing this from the QDBusMessage containing all the return values.

Once we have the QDBusMessage, we can access the return values using arguments() which returns a QList<QVariant>. m For example, for a method org.kde.DBusTute.Favourites.Get( out INT32 number, out STRING colour, out STRING flavour ), we would use the following code:


QDBusInterface iface( "org.kde.DBusTute",

                     "/org/kde/DBusTute/Favourites",
                     "org.kde.DBusTute.Favourites",
                     QDBus::sessionBus(), 0 );

QDBusMessage reply = iface.call( "Get" ); QList<QVariant> values = return.arguments(); int favouriteNumber = values.takeFirst().toInt(); QString favouriteColour = values.takeFirst().toString(); QString favouriteFlavour = values.takeFirst().toString();


KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V.Legal