Development/Tutorials/D-Bus/CustomTypes: Difference between revisions

From KDE TechBase
(converted notes and warnings from MoinMoin syntax to left indents)
(Use templates for tip and notes)
 
(15 intermediate revisions by 8 users not shown)
Line 1: Line 1:
{{Template:I18n/Language Navigation Bar|Development/Tutorials/D-Bus/Intermediate D-Bus}}
 


{{TutorialBrowser|
{{TutorialBrowser|
Line 7: Line 7:
name=Using Custom Types with DBus|
name=Using Custom Types with DBus|


pre=[[../Creating Interfaces|Creating Interfaces]]|
pre=[[../Creating Interfaces|Creating Interfaces]], [[../Intermediate_D-Bus|Intermediate DBus]]|
 
next=[[../Creating Interfaces|Creating Interfaces]]|
}}
}}


DBus-based IPC has been intregrated nicely in the Qt framework. Qt also provides [http://doc.trolltech.com/4.6/qdbusabstractadaptor.html Adaptors] and [http://doc.trolltech.com/4.6/qdbusabstractinterface.html Interfaces] to communicate with remote objects, so you don't have to manually construct [http://doc.trolltech.com/4.6/qdbusmessage.html QDBusMessages].
DBus-based IPC has been intregrated nicely in the Qt framework. Qt also provides {{qt|QDBusAbstractAdaptor}} and {{qt|QDBusAbstractInterface}} to communicate with remote objects, so you don't have to manually construct {{qt|QDBusMessage}}.
These Adaptors and Interfaces are very similar to the Stubs and Proxies used in other RPC mechanisms like [http://en.wikipedia.org/wiki/Common_Object_Request_Broker_Architecture Corba] and [http://java.sun.com/javase/technologies/core/basic/rmi/index.jsp RMI].
These Adaptors and Interfaces are very similar to the Stubs and Proxies used in other RPC mechanisms like [http://en.wikipedia.org/wiki/Common_Object_Request_Broker_Architecture Corba] and [http://java.sun.com/javase/technologies/core/basic/rmi/index.jsp RMI].


So, Qt provides a way to talk to objects in a different process and it contains Adaptors and Interfaces to do so in a nice, object-oriented way.
So, Qt provides a way to talk to objects in a different process and it contains Adaptors and Interfaces to do so in a nice, object-oriented way.
A number of types can be marshaled and unmarshaled by default, (QString, QPoint, QRect, etc.), so you can just use those in DBus-enabled classes without extra effort.
A number of types can be marshaled and unmarshaled by default, ({{qt|QString}}, {{qt|QPoint}}, {{qt|QRect}}, etc.), so you can just use those in DBus-enabled classes without extra effort.


However, using a custom type is somewhat more complicated, as you need to tell Qt how to handle it.
However, using a custom type is somewhat more complicated, as you need to tell Qt how to handle it.
That is what this tutorial is about.
That is what this tutorial is about.


An [[attachment:DBusChat.tar.bz2|example]] was created to go along with this tutorial. It is a very basic chat application that communicates through the session DBus.
An [[#Example|example]] was created to go along with this tutorial. It is a very basic chat application that communicates through the session DBus.


= Write a class =
= Write a class =
Line 29: Line 27:
The class being published in the example is the following:
The class being published in the example is the following:


<code cppqt n>
<syntaxhighlight lang="cpp-qt" line>
class Chat : public QObject
class Chat : public QObject
{
{
Line 58: Line 56:
     QStringList m_users;
     QStringList m_users;
};
};
</code>
</syntaxhighlight>


It contains simple user management and provides the means to send a message.
It contains simple user management and provides the means to send a message.
Line 73: Line 71:


Typically, the company name is included in the name, resulting in declarations like:
Typically, the company name is included in the name, resulting in declarations like:
<code cppqt>
<syntaxhighlight lang="cpp-qt">
Q_CLASSINFO("D-Bus Interface", "com.firm.department.product.interface")
Q_CLASSINFO("D-Bus Interface", "com.firm.department.product.interface")
</code>
</syntaxhighlight>


= Generate XML =
= Generate XML =
Line 81: Line 79:


If you run '''qdbuscpp2xml''' on the Chat.hpp file in the example, you will get the following output:
If you run '''qdbuscpp2xml''' on the Chat.hpp file in the example, you will get the following output:
<code xml n>
<syntaxhighlight lang="xml" line>
<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN" "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN" "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
<node>
<node>
Line 100: Line 98:
   </interface>
   </interface>
</node>
</node>
</code>
</syntaxhighlight>


Note that this xml file contains all methods from the Chat class using standard Qt types.
Note that this xml file contains all methods from the Chat class using standard Qt types.
Line 111: Line 109:
modify the XML.
modify the XML.


:'''Tip'''
{{Tip|1=You may want to generate the Adaptor and Interface using only the automatically generated xml. It might be helpful to compare them to the versions we will generate further on.}}
:
:You may want to generate the Adaptor and Interface using only the automatically generated xml. It might be helpful to compare them to the versions we will generate further on.


= Edit the XML =
= Edit the XML =
Line 120: Line 116:


This is done by specifying annotations:
This is done by specifying annotations:
<code xml n>
<syntaxhighlight lang="xml" line>
<annotation name="com.trolltech.QtDBus.QtTypeName"
<annotation name="org.qtproject.QtDBus.QtTypeName"
value="*customType*"/>
value="*customType*"/>
</code>
</syntaxhighlight>


The syntax differs slightly depending on wheter you're using it in a signal/method or in
The syntax differs slightly depending on wheter you're using it in a signal/method or in
a property (the {{{.In0}}} is omitted for properties), but it is fairly straightforward.
a property (the {{{.In0}}} is omitted for properties), but it is fairly straightforward.
The following is an example dealing with the QRect type (it has no relation with the example):
The following is an example dealing with the QRect type (it has no relation with the example):
<code xml n>
<syntaxhighlight lang="xml" line>
<property name="rectangle" type="(iiii)" access="readwrite">
<property name="rectangle" type="(iiii)" access="readwrite">
   <annotation name="com.trolltech.QtDBus.QtTypeName" value="QRect"/>
   <annotation name="org.qtproject.QtDBus.QtTypeName" value="QRect"/>
</property>
</property>


<signal name="rectangleChanged">
<signal name="rectangleChanged">
   <arg name="rect" type="(iiii)" direction="out"/>
   <arg name="rect" type="(iiii)" direction="out"/>
   <annotation name="com.trolltech.QtDBus.QtTypeName.In0" value="QRect"/>
  <!-- to support also Qt < 5.6.3 & 5.7.0, use instead "org.qtproject.QtDBus.QtTypeName.In0" -->
   <annotation name="org.qtproject.QtDBus.QtTypeName.Out0" value="QRect"/>
</signal>
</signal>


<method name="changeRectangle">
<method name="changeRectangle">
   <arg name="message" type="(iiii)" direction="in"/>
   <arg name="message" type="(iiii)" direction="in"/>
   <annotation name="com.trolltech.QtDBus.QtTypeName.In0" value="QRect"/>
   <annotation name="org.qtproject.QtDBus.QtTypeName.In0" value="QRect"/>
</method>
</method>
</code>
</syntaxhighlight>


Don't worry too much about what type to specify; any type that is
Don't worry too much about what type to specify; any type that is
Line 150: Line 147:


If any of your methods returns a complex result, you need to add an
If any of your methods returns a complex result, you need to add an
annotation for com.trolltech.QtDBus.QtTypeName.'''Out'''0 as well.
annotation for org.qtproject.QtDBus.QtTypeName.'''Out'''0 as well.
 
If any of your signals have arguments with complex types, agaiin you need to add an
annotation for org.qtproject.QtDBus.QtTypeName.'''Out'''0.
For backward-compatibility with Qt < 5.6.3  & 5.7.0 though you can use org.qtproject.QtDBus.QtTypeName.'''In'''0
as with the arguments of a normal method. qdbusxml2cpp supports that still in later versions, but will emit a warning.


:'''Note'''
{{Note|1=The XML above uses QRects. QRect is supported by default, so the code above would be generated for you by '''qdbuscpp2xml'''.}}
:
:The XML above uses QRects. QRect is supported by default, so the code above would be generated for you by '''qdbuscpp2xml'''.


The complete interface used for the Chat interface in the example is as follows:
The complete interface used for the Chat interface in the example is as follows:
<code xml n>
<syntaxhighlight lang="xml" line>
<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN" "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN" "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
<node>
<node>
Line 170: Line 170:
     <signal name="messageSent">
     <signal name="messageSent">
       <arg name="message" type="a(ii)" direction="out"/>
       <arg name="message" type="a(ii)" direction="out"/>
       <annotation name="com.trolltech.QtDBus.QtTypeName.In0" value="Message"/>
      <!-- to support also Qt < 5.6.3 & 5.7.0, use instead "org.qtproject.QtDBus.QtTypeName.In0" -->
       <annotation name="org.qtproject.QtDBus.QtTypeName.Out0" value="Message"/>
     </signal>
     </signal>
     <method name="addUser">
     <method name="addUser">
Line 180: Line 181:
     <method name="sendMessage">
     <method name="sendMessage">
       <arg name="message" type="a(ii)" direction="in"/>
       <arg name="message" type="a(ii)" direction="in"/>
       <annotation name="com.trolltech.QtDBus.QtTypeName.In0" value="Message"/>
       <annotation name="org.qtproject.QtDBus.QtTypeName.In0" value="Message"/>
     </method>
     </method>
   </interface>
   </interface>
</node>
</node>
</code>
</syntaxhighlight>


= Generate Adaptor and Interface classes =
= Generate Adaptor and Interface classes =
Line 196: Line 197:
For the DBusChat example, the Adaptor and Interface classes were created
For the DBusChat example, the Adaptor and Interface classes were created
by calling
by calling
<code>
<syntaxhighlight lang="bash">
qdbusxml2cpp Chat.xml -i Message.hpp -a ChatAdaptor
qdbusxml2cpp Chat.xml -i Message.hpp -a ChatAdaptor
qdbusxml2cpp Chat.xml -i Message.hpp -p ChatInterface
qdbusxml2cpp Chat.xml -i Message.hpp -p ChatInterface
</code>
</syntaxhighlight>


= Qt Meta object magic =
= Qt Meta object magic =
Line 209: Line 210:
== Register the type ==
== Register the type ==
Declare the type as a Qt meta type by adding a
Declare the type as a Qt meta type by adding a
<code cppqt> Q_DECLARE_METATYPE </code> statement to the header file containing
<syntaxhighlight lang="cpp-qt"> Q_DECLARE_METATYPE </syntaxhighlight> statement to the header file containing
your custom type definition.
your custom type definition.


Line 216: Line 217:


In the example's Message class, a static method is included to do this:
In the example's Message class, a static method is included to do this:
<code cppqt n>
<syntaxhighlight lang="cpp-qt" line>
void Message::registerMetaType()
void Message::registerMetaType()
{
{
Line 223: Line 224:
     qDBusRegisterMetaType<Message>();
     qDBusRegisterMetaType<Message>();
}
}
</code>
</syntaxhighlight>


:'''Important'''
:'''Important'''
Line 244: Line 245:
For the Message type from the example, these operators are very simple,
For the Message type from the example, these operators are very simple,
since it only contains 2 strings:
since it only contains 2 strings:
<code cppqt n>
<syntaxhighlight lang="cpp-qt" line>
QDBusArgument &operator<<(QDBusArgument &argument, const Message& message)
QDBusArgument &operator<<(QDBusArgument &argument, const Message& message)
{
{
Line 264: Line 265:
     return argument;
     return argument;
}
}
</code>
</syntaxhighlight>
QDBusArgument provides functions to handle much more complex types
QDBusArgument provides functions to handle much more complex types
as well.
as well.
Line 278: Line 279:
This is the code that publishes a Chat object in the DBusChat
This is the code that publishes a Chat object in the DBusChat
example:
example:
<code cppqt n>
<syntaxhighlight lang="cpp-qt" line>
Chat* pChat = new Chat(&a);
Chat* pChat = new Chat(&a);
ChatAdaptor* pChatAdaptor = new ChatAdaptor(pChat);
ChatAdaptor* pChatAdaptor = new ChatAdaptor(pChat);
Line 291: Line 292:
     qFatal("Could not register Chat object!");
     qFatal("Could not register Chat object!");
}
}
</code>
</syntaxhighlight>


The ChatAdaptor instance will process any incoming DBus
The ChatAdaptor instance will process any incoming DBus
Line 306: Line 307:
In the DBusChat example, a connection is made with a
In the DBusChat example, a connection is made with a
remote Chat object like this:
remote Chat object like this:
<code cppqt>
<syntaxhighlight lang="cpp-qt">
demo::Chat chatInterface(CHAT_SERVICE, CHAT_PATH, connection);
demo::Chat chatInterface(CHAT_SERVICE, CHAT_PATH, connection);
</code>
</syntaxhighlight>


== Using the remote object in your application ==
== Using the remote object in your application ==
Line 317: Line 318:
The ChatWindow class in the DBusChat example, for
The ChatWindow class in the DBusChat example, for
instance, adds a user simply by calling
instance, adds a user simply by calling
<code cppqt>
<syntaxhighlight lang="cpp-qt">
m_chatInterface.addUser(m_userName);
m_chatInterface.addUser(m_userName);
</code>
</syntaxhighlight>


Furthermore, ChatWindow is able to respond to signals
Furthermore, ChatWindow is able to respond to signals
emitted by the Chat object by connecting to its
emitted by the Chat object by connecting to its
Interface class just like with any other QObject:
Interface class just like with any other QObject:
<code cppqt>
<syntaxhighlight lang="cpp-qt">
connect(&m_chatInterface, SIGNAL(userAdded(QString)), SLOT(chat_userAdded(QString)));
connect(&m_chatInterface, SIGNAL(userAdded(QString)), SLOT(chat_userAdded(QString)));
connect(&m_chatInterface, SIGNAL(userRemoved(QString)), SLOT(chat_userRemoved(QString)));
connect(&m_chatInterface, SIGNAL(userRemoved(QString)), SLOT(chat_userRemoved(QString)));
connect(&m_chatInterface, SIGNAL(messageSent(Message)), SLOT(chat_messageSent(Message)));
connect(&m_chatInterface, SIGNAL(messageSent(Message)), SLOT(chat_messageSent(Message)));
</code>
</syntaxhighlight>


= Varia =
= Varia =
Line 342: Line 343:
the header files passed to them, so they would be able to recognize the
the header files passed to them, so they would be able to recognize the
custom types and add methods/signals/properties to the XML accordingly.
custom types and add methods/signals/properties to the XML accordingly.
One potential strategy for extending '''qdbuscpp2xml''' is with plugins,
and this is presented at [[Development/Tutorials/D-Bus/Cpp2XmlPlugins|Cpp2XmlPlugins]].


Alternatively, a feature could be added to Qt Creator to support the kind
Alternatively, a feature could be added to Qt Creator to support the kind
Line 353: Line 356:
enumeration. A basic implementation could simply cast the enum to and from an int,
enumeration. A basic implementation could simply cast the enum to and from an int,
resulting in code looking like this:
resulting in code looking like this:
<code cppqt n>
<syntaxhighlight lang="cpp-qt" line>
QDBusArgument &operator<<(QDBusArgument &argument, const EnumerationType& source)
QDBusArgument &operator<<(QDBusArgument &argument, const EnumerationType& source)
{
{
Line 373: Line 376:
     return argument;
     return argument;
}
}
</code>
</syntaxhighlight>


You will notice that this code will be much the same for all
You will notice that this code will be much the same for all
Line 395: Line 398:
This is what the marshaling and unmarshaling code actually
This is what the marshaling and unmarshaling code actually
looks like:
looks like:
<code cppqt n>
<syntaxhighlight lang="cpp-qt" line>
template<typename T, typename TEnum>
template<typename T, typename TEnum>
class QDBusEnumMarshal;
class QDBusEnumMarshal;
Line 423: Line 426:
}
}
}
}
</code>
</syntaxhighlight>


The marshal implementation can now be called by invoking:
The marshal implementation can now be called by invoking:
<code cppqt>
<syntaxhighlight lang="cpp-qt">
QDBusEnumMarshal<T, typename boost::is_enum<T>::type>::marshal(argument, source);
QDBusEnumMarshal<T, typename boost::is_enum<T>::type>::marshal(argument, source);
</code>
</syntaxhighlight>
with T being the type we want to marshal.
with T being the type we want to marshal.


Some further glue code is needed to link the implementation to
Some further glue code is needed to link the implementation to
the QDbusArgument stream operators:
the QDbusArgument stream operators:
<code cppqt n>
<syntaxhighlight lang="cpp-qt" line>
template<typename T>
template<typename T>
QDBusArgument& operator<<(QDBusArgument &argument, const T& source)
QDBusArgument& operator<<(QDBusArgument &argument, const T& source)
Line 445: Line 448:
     return QDBusEnumMarshal<T, typename boost::is_enum<T>::type>::unmarshal(argument, source);
     return QDBusEnumMarshal<T, typename boost::is_enum<T>::type>::unmarshal(argument, source);
}
}
</code>
</syntaxhighlight>


Put all that in [[attachment:enumDBus.hpp|a header file]] and include it where you declare the
Put all that in [[#enumDBus.hpp|a header file]] and include it where you declare the
enumerations that need to pass across DBus. The compiler is now able
enumerations that need to pass across DBus. The compiler is now able
to find the streaming operators all on its own.
to find the streaming operators all on its own.
Line 454: Line 457:
register it with the Qt meta object system though.
register it with the Qt meta object system though.


:'''Note'''
{{Note|1=You only need a boost header file to do this ({{path|boost/type_traits/is_enum.hpp}}, to be exact). There is no need to have the entire library installed, which is rather nice on an embedded platform, since the boost library takes up quite a bit of space.}}
:
 
:You only need a boost header file to do this ('''boost/type_traits/is_enum.hpp''', to be exact). There is no need to have the entire library installed, which is rather nice on an embedded platform, since the boost library takes up quite a bit of space.
=== enumDBus.hpp ===
 
This is the entire header file needed to marshal / unmarshal any enumeration:
<syntaxhighlight lang="cpp-qt">
#ifndef _ENUM_DBUS_HPP
#define _ENUM_DBUS_HPP
 
#include <QtDBus/QDBusArgument>
 
#include <boost/type_traits/is_enum.hpp>
 
using namespace std;
 
template<typename T, typename TEnum>
class QDBusEnumMarshal;
 
template<typename T>
class QDBusEnumMarshal<T, boost::true_type>
{
public:
static QDBusArgument& marshal(QDBusArgument &argument, const T& source)
{
argument.beginStructure();
argument << static_cast<int>(source);
argument.endStructure();
return argument;
}
 
static const QDBusArgument& unmarshal(const QDBusArgument &argument, T &source)
{
int a;
argument.beginStructure();
argument >> a;
argument.endStructure();
source = static_cast<T>(a);
return argument;
}
};
 
template<typename T>
QDBusArgument& operator<<(QDBusArgument &argument, const T& source)
{
return QDBusEnumMarshal<T, typename boost::is_enum<T>::type>::marshal(argument, source);
}
 
template<typename T>
const QDBusArgument& operator>>(const QDBusArgument &argument, T &source)
{
return QDBusEnumMarshal<T, typename boost::is_enum<T>::type>::unmarshal(argument, source);
}
 
#endif //_ENUM_DBUS_HPP
</syntaxhighlight>
 
= Example =
 
This section contains the sources for the chat example. The wiki does not allow the upload of files other than images, so they've been posted as text.
 
Chat is the interface being published. ChatAdaptor and ChatInterface were generated using Chat.xml.
 
Put all these files in a directory and run
<syntaxhighlight lang="bash">
qmake
 
make
</syntaxhighlight>
 
== Chat.hpp ==
<syntaxhighlight lang="cpp-qt">
#ifndef CHAT_HPP
#define CHAT_HPP
 
#include <QObject>
#include <QStringList>
 
class Message;
 
class Chat : public QObject
{
    Q_OBJECT
 
    Q_CLASSINFO("D-Bus Interface", "demo.Chat")
    Q_PROPERTY( QStringList users READ users)
 
signals:
    void userAdded(const QString& user);
    void userRemoved(const QString& user);
 
    void messageSent(const Message &message);
 
public slots:
    void addUser(const QString &user);
    void removeUser(const QString &user);
 
    void sendMessage(const Message &message);
 
public:
    Chat(QObject* parent = 0);
    virtual ~Chat();
 
    QStringList users() const;
 
private:
    QStringList m_users;
};
#endif // CHAT_HPP
</syntaxhighlight>
 
== Chat.cpp ==
<syntaxhighlight lang="cpp-qt">
#include "Chat.hpp"
 
Chat::Chat(QObject* parent) :
        QObject(parent)
{
}
 
Chat::~Chat()
{
}
 
void Chat::addUser(const QString &user)
{
    if (!m_users.contains(user))
    {
        m_users.append(user);
 
        emit userAdded(user);
    }
}
 
void Chat::removeUser(const QString &user)
{
    if (m_users.contains(user))
    {
        m_users.removeOne(user);
 
        emit userRemoved(user);
    }
}
 
void Chat::sendMessage(const Message &message)
{
    Q_EMIT messageSent(message);
}
 
QStringList Chat::users() const
{
    return m_users;
}
</syntaxhighlight>
 
== Chat.xml ==
<syntaxhighlight lang="xml">
<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN" "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
<node>
  <interface name="demo.Chat">
    <property name="users" type="as" access="read"/>
    <signal name="userAdded">
      <arg name="user" type="s" direction="out"/>
    </signal>
    <signal name="userRemoved">
      <arg name="user" type="s" direction="out"/>
    </signal>
    <signal name="messageSent">
      <arg name="message" type="a(ii)" direction="out"/>
      <!-- to support also Qt < 5.6.3 & 5.7.0, use instead "org.qtproject.QtDBus.QtTypeName.In0" -->
      <annotation name="com.trolltech.QtDBus.QtTypeName.Out0" value="Message"/>
    </signal>
    <method name="addUser">
      <arg name="user" type="s" direction="in"/>
    </method>
    <method name="removeUser">
      <arg name="user" type="s" direction="in"/>
    </method>
    <method name="sendMessage">
      <arg name="message" type="a(ii)" direction="in"/>
      <annotation name="com.trolltech.QtDBus.QtTypeName.In0" value="Message"/>
    </method>
  </interface>
</node>
</syntaxhighlight>
 
== ChatAdaptor.h ==
<syntaxhighlight lang="cpp-qt">
/*
* This file was generated by qdbusxml2cpp version 0.7
* Command line was: qdbusxml2cpp Chat.xml -i Message.hpp -a ChatAdaptor
*
* qdbusxml2cpp is Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
*
* This is an auto-generated file.
* This file may have been hand-edited. Look for HAND-EDIT comments
* before re-generating it.
*/
 
#ifndef CHATADAPTOR_H_1270658274
#define CHATADAPTOR_H_1270658274
 
#include <QtCore/QObject>
#include <QtDBus/QtDBus>
#include "Message.hpp"
class QByteArray;
template<class T> class QList;
template<class Key, class Value> class QMap;
class QString;
class QStringList;
class QVariant;
 
/*
* Adaptor class for interface demo.Chat
*/
class ChatAdaptor: public QDBusAbstractAdaptor
{
    Q_OBJECT
    Q_CLASSINFO("D-Bus Interface", "demo.Chat")
    Q_CLASSINFO("D-Bus Introspection", ""
"  <interface name=\"demo.Chat\">\n"
"    <property access=\"read\" type=\"as\" name=\"users\"/>\n"
"    <signal name=\"userAdded\">\n"
"      <arg direction=\"out\" type=\"s\" name=\"user\"/>\n"
"    </signal>\n"
"    <signal name=\"userRemoved\">\n"
"      <arg direction=\"out\" type=\"s\" name=\"user\"/>\n"
"    </signal>\n"
"    <signal name=\"messageSent\">\n"
"      <arg direction=\"out\" type=\"a(ii)\" name=\"message\"/>\n"
"      <!-- to support also Qt < 5.6.3 & 5.7.0, use instead "org.qtproject.QtDBus.QtTypeName.In0" -->\n"
"      <annotation value=\"Message\" name=\"com.trolltech.QtDBus.QtTypeName.Out0\"/>\n"
"    </signal>\n"
"    <method name=\"addUser\">\n"
"      <arg direction=\"in\" type=\"s\" name=\"user\"/>\n"
"    </method>\n"
"    <method name=\"removeUser\">\n"
"      <arg direction=\"in\" type=\"s\" name=\"user\"/>\n"
"    </method>\n"
"    <method name=\"sendMessage\">\n"
"      <arg direction=\"in\" type=\"a(ii)\" name=\"message\"/>\n"
"      <annotation value=\"Message\" name=\"com.trolltech.QtDBus.QtTypeName.In0\"/>\n"
"    </method>\n"
"  </interface>\n"
        "")
public:
    ChatAdaptor(QObject *parent);
    virtual ~ChatAdaptor();
 
public: // PROPERTIES
    Q_PROPERTY(QStringList users READ users)
    QStringList users() const;
 
public Q_SLOTS: // METHODS
    void addUser(const QString &user);
    void removeUser(const QString &user);
    void sendMessage(Message message);
Q_SIGNALS: // SIGNALS
    void messageSent(Message message);
    void userAdded(const QString &user);
    void userRemoved(const QString &user);
};
 
#endif
</syntaxhighlight>
 
== ChatAdaptor.cpp ==
<syntaxhighlight lang="cpp-qt">
/*
* This file was generated by qdbusxml2cpp version 0.7
* Command line was: qdbusxml2cpp Chat.xml -i Message.hpp -a ChatAdaptor
*
* qdbusxml2cpp is Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
*
* This is an auto-generated file.
* Do not edit! All changes made to it will be lost.
*/
 
#include "ChatAdaptor.h"
#include <QtCore/QMetaObject>
#include <QtCore/QByteArray>
#include <QtCore/QList>
#include <QtCore/QMap>
#include <QtCore/QString>
#include <QtCore/QStringList>
#include <QtCore/QVariant>
 
/*
* Implementation of adaptor class ChatAdaptor
*/
 
ChatAdaptor::ChatAdaptor(QObject *parent)
    : QDBusAbstractAdaptor(parent)
{
    // constructor
    setAutoRelaySignals(true);
}
 
ChatAdaptor::~ChatAdaptor()
{
    // destructor
}
 
QStringList ChatAdaptor::users() const
{
    // get the value of property users
    return qvariant_cast< QStringList >(parent()->property("users"));
}
 
void ChatAdaptor::addUser(const QString &user)
{
    // handle method call demo.Chat.addUser
    QMetaObject::invokeMethod(parent(), "addUser", Q_ARG(QString, user));
}
 
void ChatAdaptor::removeUser(const QString &user)
{
    // handle method call demo.Chat.removeUser
    QMetaObject::invokeMethod(parent(), "removeUser", Q_ARG(QString, user));
}
 
void ChatAdaptor::sendMessage(Message message)
{
    // handle method call demo.Chat.sendMessage
    QMetaObject::invokeMethod(parent(), "sendMessage", Q_ARG(Message, message));
}
</syntaxhighlight>
 
== ChatInterface.h ==
<syntaxhighlight lang="cpp-qt">
/*
* This file was generated by qdbusxml2cpp version 0.7
* Command line was: qdbusxml2cpp Chat.xml -i Message.hpp -p ChatInterface
*
* qdbusxml2cpp is Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
*
* This is an auto-generated file.
* Do not edit! All changes made to it will be lost.
*/
 
#ifndef CHATINTERFACE_H_1270658265
#define CHATINTERFACE_H_1270658265
 
#include <QtCore/QObject>
#include <QtCore/QByteArray>
#include <QtCore/QList>
#include <QtCore/QMap>
#include <QtCore/QString>
#include <QtCore/QStringList>
#include <QtCore/QVariant>
#include <QtDBus/QtDBus>
#include "Message.hpp"
 
/*
* Proxy class for interface demo.Chat
*/
class DemoChatInterface: public QDBusAbstractInterface
{
    Q_OBJECT
public:
    static inline const char *staticInterfaceName()
    { return "demo.Chat"; }
 
public:
    DemoChatInterface(const QString &service, const QString &path, const QDBusConnection &connection, QObject *parent = 0);
 
    ~DemoChatInterface();
 
    Q_PROPERTY(QStringList users READ users)
    inline QStringList users() const
    { return qvariant_cast< QStringList >(property("users")); }
 
public Q_SLOTS: // METHODS
    inline QDBusPendingReply<> addUser(const QString &user)
    {
        QList<QVariant> argumentList;
        argumentList << qVariantFromValue(user);
        return asyncCallWithArgumentList(QLatin1String("addUser"), argumentList);
    }
 
    inline QDBusPendingReply<> removeUser(const QString &user)
    {
        QList<QVariant> argumentList;
        argumentList << qVariantFromValue(user);
        return asyncCallWithArgumentList(QLatin1String("removeUser"), argumentList);
    }
 
    inline QDBusPendingReply<> sendMessage(Message message)
    {
        QList<QVariant> argumentList;
        argumentList << qVariantFromValue(message);
        return asyncCallWithArgumentList(QLatin1String("sendMessage"), argumentList);
    }
 
Q_SIGNALS: // SIGNALS
    void messageSent(Message message);
    void userAdded(const QString &user);
    void userRemoved(const QString &user);
};
 
namespace demo {
  typedef ::DemoChatInterface Chat;
}
#endif
</syntaxhighlight>
 
== ChatInterface.cpp ==
<syntaxhighlight lang="cpp-qt">
/*
* This file was generated by qdbusxml2cpp version 0.7
* Command line was: qdbusxml2cpp Chat.xml -i Message.hpp -p ChatInterface
*
* qdbusxml2cpp is Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
*
* This is an auto-generated file.
* This file may have been hand-edited. Look for HAND-EDIT comments
* before re-generating it.
*/
 
#include "ChatInterface.h"
 
/*
* Implementation of interface class DemoChatInterface
*/
 
DemoChatInterface::DemoChatInterface(const QString &service, const QString &path, const QDBusConnection &connection, QObject *parent)
    : QDBusAbstractInterface(service, path, staticInterfaceName(), connection, parent)
{
}
 
DemoChatInterface::~DemoChatInterface()
{
}
</syntaxhighlight>
 
== ChatWindow.hpp ==
<syntaxhighlight lang="cpp-qt">
#ifndef CHATWINDOW_HPP
#define CHATWINDOW_HPP
 
#include <QtGui/QMainWindow>
#include <QtGui/QStringListModel>
 
#include "ChatInterface.h"
 
namespace Ui
{
    class ChatWindow;
}
 
class QCloseEvent;
 
class ChatWindow : public QMainWindow
{
    Q_OBJECT
 
public:
    ChatWindow(demo::Chat& chatInterface, QWidget *parent = 0);
    virtual ~ChatWindow();
 
protected:
    virtual void closeEvent(QCloseEvent *event);
 
private:
    ChatWindow(const ChatWindow &other);
    ChatWindow& operator=(const ChatWindow &other);
 
    Ui::ChatWindow *ui;
 
    QString m_userName;
 
    QStringListModel m_users;
 
    demo::Chat &m_chatInterface;
 
private slots:
    void sendMessage();
 
    void chat_userAdded(const QString &user);
    void chat_userRemoved(const QString &user);
    void chat_messageSent(const Message &message);
};
 
#endif // CHATWINDOW_HPP
</syntaxhighlight>
 
== ChatWindow.cpp ==
<syntaxhighlight lang="cpp-qt">
#include "ChatWindow.hpp"
#include "ui_ChatWindow.h"
 
#include <QtGui/QCloseEvent>
#include <QtGui/QInputDialog>
#include <QtGui/QMessageBox>
 
#include "Message.hpp"
 
ChatWindow::ChatWindow(demo::Chat& chatInterface, QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::ChatWindow),
    m_userName(),
    m_users(),
    m_chatInterface(chatInterface)
{
    ui->setupUi(this);
 
    QStringList userList = chatInterface.users();
    m_users.setStringList(userList);
    m_users.sort(0);
 
    connect(&m_chatInterface, SIGNAL(userAdded(QString)), SLOT(chat_userAdded(QString)));
    connect(&m_chatInterface, SIGNAL(userRemoved(QString)), SLOT(chat_userRemoved(QString)));
    connect(&m_chatInterface, SIGNAL(messageSent(Message)), SLOT(chat_messageSent(Message)));
 
    connect(ui->lneMessage, SIGNAL(returnPressed()), SLOT(sendMessage()));
    connect(ui->btnSend, SIGNAL(released()), SLOT(sendMessage()));
 
    bool ok;
    m_userName = QInputDialog::getText(this, "Username?", "User name:", QLineEdit::Normal, QDir::home().dirName(), &ok, Qt::Dialog);
 
    if (!ok)
    {
        QApplication::exit();
        qFatal("Cancelled");
    }
 
    if (userList.contains(m_userName))
    {
        QMessageBox::critical(this, "Invalid Username", "Username " + m_userName + " is already taken");
        QApplication::exit();
        qFatal("Duplicate username");
    }
 
    m_chatInterface.addUser(m_userName);
 
    setWindowTitle("Chat -- " + m_userName);
 
    ui->lstUsers->setModel(&m_users);
}
 
ChatWindow::~ChatWindow()
{
    delete ui;
}
 
void ChatWindow::closeEvent(QCloseEvent *event)
{
    QMainWindow::closeEvent(event);
 
    if (event->isAccepted())
    {
        m_chatInterface.removeUser(m_userName);
    }
}
 
void ChatWindow::sendMessage()
{
    Message message(m_userName, ui->lneMessage->text());
    m_chatInterface.sendMessage(message);
 
    ui->lneMessage->clear();
}
 
void ChatWindow::chat_userAdded(const QString &user)
{
    QStringList users = m_users.stringList();
    users.append(user);
    users.sort();
    m_users.setStringList(users);
}
 
void ChatWindow::chat_userRemoved(const QString &user)
{
    QStringList users = m_users.stringList();
    users.removeOne(user);
    m_users.setStringList(users);
}
 
void ChatWindow::chat_messageSent(const Message &message)
{
    ui->txtChat->appendPlainText(message.getUser() + " : " + message.getText());
}
</syntaxhighlight>
 
== ChatWindow.ui ==
<syntaxhighlight lang="xml">
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>ChatWindow</class>
<widget class="QMainWindow" name="ChatWindow">
  <property name="geometry">
  <rect>
    <x>0</x>
    <y>0</y>
    <width>600</width>
    <height>400</height>
  </rect>
  </property>
  <property name="windowTitle">
  <string>ChatWindow</string>
  </property>
  <widget class="QWidget" name="centralWidget">
  <layout class="QVBoxLayout" name="verticalLayout">
    <item>
    <widget class="QWidget" name="widget_2" native="true">
      <layout class="QHBoxLayout" name="horizontalLayout_2">
      <item>
        <widget class="QListView" name="lstUsers">
        <property name="maximumSize">
          <size>
          <width>150</width>
          <height>16777215</height>
          </size>
        </property>
        <property name="focusPolicy">
          <enum>Qt::NoFocus</enum>
        </property>
        <property name="editTriggers">
          <set>QAbstractItemView::NoEditTriggers</set>
        </property>
        <property name="selectionMode">
          <enum>QAbstractItemView::NoSelection</enum>
        </property>
        </widget>
      </item>
      <item>
        <widget class="QPlainTextEdit" name="txtChat">
        <property name="focusPolicy">
          <enum>Qt::NoFocus</enum>
        </property>
        </widget>
      </item>
      </layout>
    </widget>
    </item>
    <item>
    <widget class="QWidget" name="widget" native="true">
      <layout class="QHBoxLayout" name="horizontalLayout">
      <item>
        <widget class="QLineEdit" name="lneMessage"/>
      </item>
      <item>
        <widget class="QPushButton" name="btnSend">
        <property name="text">
          <string>Send</string>
        </property>
        </widget>
      </item>
      </layout>
    </widget>
    </item>
  </layout>
  </widget>
  <widget class="QMenuBar" name="menuBar">
  <property name="geometry">
    <rect>
    <x>0</x>
    <y>0</y>
    <width>600</width>
    <height>25</height>
    </rect>
  </property>
  </widget>
  <widget class="QToolBar" name="mainToolBar">
  <attribute name="toolBarArea">
    <enum>TopToolBarArea</enum>
  </attribute>
  <attribute name="toolBarBreak">
    <bool>false</bool>
  </attribute>
  </widget>
  <widget class="QStatusBar" name="statusBar"/>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
</syntaxhighlight>
 
== Message.hpp ==
<syntaxhighlight lang="cpp-qt">
#ifndef MESSAGE_HPP
#define MESSAGE_HPP
 
#include <QtDBus>
 
class Message
{
public:
    Message();
    Message(const QString& user, const QString &message);
    Message(const Message &other);
    Message& operator=(const Message &other);
    ~Message();
 
    friend QDBusArgument &operator<<(QDBusArgument &argument, const Message &message);
    friend const QDBusArgument &operator>>(const QDBusArgument &argument, Message &message);
 
    QString getUser() const;
    QString getText() const;
 
    //register Message with the Qt type system
    static void registerMetaType();
 
private:
    QString m_user;
    QString m_text;
};
 
Q_DECLARE_METATYPE(Message)
 
#endif // MESSAGE_HPP
</syntaxhighlight>
 
== Message.cpp ==
<syntaxhighlight lang="cpp-qt">
#include "Message.hpp"
 
Message::Message() :
        m_user(),
        m_text()
{
}
 
Message::Message(const QString &user, const QString &text) :
        m_user(user),
        m_text(text)
{
}
 
Message::Message(const Message &other) :
        m_user(other.m_user),
        m_text(other.m_text)
{
}
 
Message& Message::operator=(const Message &other)
{
    m_user = other.m_user;
    m_text = other.m_text;
 
    return *this;
}
 
Message::~Message()
{
}
 
QString Message::getUser() const
{
    return m_user;
}
 
QString Message::getText() const
{
    return m_text;
}
 
void Message::registerMetaType()
{
    qRegisterMetaType<Message>("Message");
 
    qDBusRegisterMetaType<Message>();
}
 
QDBusArgument &operator<<(QDBusArgument &argument, const Message& message)
{
    argument.beginStructure();
    argument << message.m_user;
    argument << message.m_text;
    argument.endStructure();
 
    return argument;
}
 
const QDBusArgument &operator>>(const QDBusArgument &argument, Message &message)
{
    argument.beginStructure();
    argument >> message.m_user;
    argument >> message.m_text;
    argument.endStructure();
 
    return argument;
}
</syntaxhighlight>
 
== main.cpp ==
<syntaxhighlight lang="cpp-qt">
#include <QtDBus/QDBusConnection>
#include <QtDBus/QDBusConnectionInterface>
#include <QtGui/QApplication>
 
#include "Chat.hpp"
#include "ChatAdaptor.h"
#include "ChatInterface.h"
#include "ChatWindow.hpp"
#include "Message.hpp"
 
#define CHAT_SERVICE "demo.dbus.chat"
#define CHAT_PATH "/chat"
 
int main(int argc, char *argv[])
{
    /*
      Register the Message type first thing, so Qt knows how to handle
      it before an Adaptor/Interface is even constructed.
 
      It should be ok to register it further down, as long as no
      Message marshaling or unmarshaling takes place, but this is
      definitely the safest way of doing things.
    */
    Message::registerMetaType();
 
    QApplication a(argc, argv);
    Chat* pChat = NULL;
    ChatAdaptor* pChatAdaptor = NULL;
 
    /*
      Create a Chat instance and register it with the session bus only if
      the service isn't already available.
    */
    QDBusConnection connection = QDBusConnection::sessionBus();
    if (!connection.interface()->isServiceRegistered(CHAT_SERVICE))
    {
        pChat = new Chat(&a);
        pChatAdaptor = new ChatAdaptor(pChat);
 
        if (!connection.registerService(CHAT_SERVICE))
        {
            qFatal("Could not register service!");
        }
 
        if (!connection.registerObject(CHAT_PATH, pChat))
        {
            qFatal("Could not register Chat object!");
        }
    }
 
    demo::Chat chatInterface(CHAT_SERVICE, CHAT_PATH, connection);
 
    ChatWindow w(chatInterface);
    w.show();
 
    int ret = a.exec();
 
    //cleanup
    if (pChat)
    {
        delete pChat;
    }
    if (pChatAdaptor)
    {
        delete pChatAdaptor;
    }
 
    return ret;
}
</syntaxhighlight>
 
== DBusChat.pro ==
<syntaxhighlight lang="bash">
# -------------------------------------------------
# Project created by QtCreator 2010-04-06T21:08:22
# -------------------------------------------------
QT += dbus
TARGET = DBusChat
TEMPLATE = app
SOURCES += main.cpp \
    ChatWindow.cpp \
    Chat.cpp \
    Message.cpp \
    ChatAdaptor.cpp \
    ChatInterface.cpp
HEADERS += ChatWindow.hpp \
    Chat.hpp \
    Message.hpp \
    ChatAdaptor.h \
    ChatInterface.h
FORMS += ChatWindow.ui
</syntaxhighlight>

Latest revision as of 12:31, 17 April 2020


Using Custom Types with DBus
Tutorial Series   D-Bus
Previous   Creating Interfaces, Intermediate DBus
What's Next   n/a
Further Reading   n/a

DBus-based IPC has been intregrated nicely in the Qt framework. Qt also provides QDBusAbstractAdaptor and QDBusAbstractInterface to communicate with remote objects, so you don't have to manually construct QDBusMessage. These Adaptors and Interfaces are very similar to the Stubs and Proxies used in other RPC mechanisms like Corba and RMI.

So, Qt provides a way to talk to objects in a different process and it contains Adaptors and Interfaces to do so in a nice, object-oriented way. A number of types can be marshaled and unmarshaled by default, (QString, QPoint, QRect, etc.), so you can just use those in DBus-enabled classes without extra effort.

However, using a custom type is somewhat more complicated, as you need to tell Qt how to handle it. That is what this tutorial is about.

An example was created to go along with this tutorial. It is a very basic chat application that communicates through the session DBus.

Write a class

Write the class you would like to publish, complete with signals, slots and properties, using custom types as you go along.

The class being published in the example is the following:

class Chat : public QObject
{
    Q_OBJECT

    Q_CLASSINFO("D-Bus Interface", "demo.Chat")
    Q_PROPERTY( QStringList users READ users)

signals:
    void userAdded(const QString& user);
    void userRemoved(const QString& user);

    void messageSent(const Message &message);

public slots:
    void addUser(const QString &user);
    void removeUser(const QString &user);

    void sendMessage(const Message &message);

public:
    Chat(QObject* parent = 0);
    virtual ~Chat();

    QStringList users() const;

private:
    QStringList m_users;
};

It contains simple user management and provides the means to send a message. The Message class is the custom type in the example. It contains only a user and a text message. There is no reason why the methods in the Chat class couldn't just take 2 QString parameters, but then Qt would be able to do everything automatically and we need something irregular for this tutorial.

To show that Qt supports a lot of types right out of the box, a QStringList was used. This will become clear further on.

Q_CLASSINFO

The Q_CLASSINFO declaration provides a means to specify an interface name that will be take into account by the xml tools.

Typically, the company name is included in the name, resulting in declarations like:

Q_CLASSINFO("D-Bus Interface", "com.firm.department.product.interface")

Generate XML

Generate the xml description of the class using the qdbuscpp2xml tool provided by Qt.

If you run qdbuscpp2xml on the Chat.hpp file in the example, you will get the following output:

<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN" "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
<node>
  <interface name="demo.Chat">
    <property name="users" type="as" access="read"/>
    <signal name="userAdded">
      <arg name="user" type="s" direction="out"/>
    </signal>
    <signal name="userRemoved">
      <arg name="user" type="s" direction="out"/>
    </signal>
    <method name="addUser">
      <arg name="user" type="s" direction="in"/>
    </method>
    <method name="removeUser">
      <arg name="user" type="s" direction="in"/>
    </method>
  </interface>
</node>

Note that this xml file contains all methods from the Chat class using standard Qt types. If you were to generate Adaptor and Interface classes based on this xml, you would be able to add and remove users, get the list of users and receive the userAdded and userRemoved signals. Even the QStringList type is handled automatically.

The methods dealing with the Message type, however, are not available. To generate an Adaptor and an Interface capable of dealing with the Message type, you need to modify the XML.

Tip
You may want to generate the Adaptor and Interface using only the automatically generated xml. It might be helpful to compare them to the versions we will generate further on.


Edit the XML

The qdbusxml2cpp tool needs to be told about the custom types, so you need to add some type information to the XML.

This is done by specifying annotations:

<annotation name="org.qtproject.QtDBus.QtTypeName"
value="*customType*"/>

The syntax differs slightly depending on wheter you're using it in a signal/method or in a property (the {{{.In0}}} is omitted for properties), but it is fairly straightforward. The following is an example dealing with the QRect type (it has no relation with the example):

<property name="rectangle" type="(iiii)" access="readwrite">
  <annotation name="org.qtproject.QtDBus.QtTypeName" value="QRect"/>
</property>

<signal name="rectangleChanged">
  <arg name="rect" type="(iiii)" direction="out"/>
  <!-- to support also Qt < 5.6.3 & 5.7.0, use instead "org.qtproject.QtDBus.QtTypeName.In0" -->
  <annotation name="org.qtproject.QtDBus.QtTypeName.Out0" value="QRect"/>
</signal>

<method name="changeRectangle">
  <arg name="message" type="(iiii)" direction="in"/>
  <annotation name="org.qtproject.QtDBus.QtTypeName.In0" value="QRect"/>
</method>

Don't worry too much about what type to specify; any type that is somewhat too complex to get marshaled/unmarshaled by default handlers will be processed using the custom type, so it really doesn't matter what type you use. You could even use {{{"(iiii)"}}} for everything.

If any of your methods returns a complex result, you need to add an annotation for org.qtproject.QtDBus.QtTypeName.Out0 as well.

If any of your signals have arguments with complex types, agaiin you need to add an annotation for org.qtproject.QtDBus.QtTypeName.Out0. For backward-compatibility with Qt < 5.6.3 & 5.7.0 though you can use org.qtproject.QtDBus.QtTypeName.In0 as with the arguments of a normal method. qdbusxml2cpp supports that still in later versions, but will emit a warning.

Note
The XML above uses QRects. QRect is supported by default, so the code above would be generated for you by qdbuscpp2xml.


The complete interface used for the Chat interface in the example is as follows:

<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN" "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
<node>
  <interface name="demo.Chat">
    <property name="users" type="as" access="read"/>
    <signal name="userAdded">
      <arg name="user" type="s" direction="out"/>
    </signal>
    <signal name="userRemoved">
      <arg name="user" type="s" direction="out"/>
    </signal>
    <signal name="messageSent">
      <arg name="message" type="a(ii)" direction="out"/>
      <!-- to support also Qt < 5.6.3 & 5.7.0, use instead "org.qtproject.QtDBus.QtTypeName.In0" -->
      <annotation name="org.qtproject.QtDBus.QtTypeName.Out0" value="Message"/>
    </signal>
    <method name="addUser">
      <arg name="user" type="s" direction="in"/>
    </method>
    <method name="removeUser">
      <arg name="user" type="s" direction="in"/>
    </method>
    <method name="sendMessage">
      <arg name="message" type="a(ii)" direction="in"/>
      <annotation name="org.qtproject.QtDBus.QtTypeName.In0" value="Message"/>
    </method>
  </interface>
</node>

Generate Adaptor and Interface classes

Now that the XML contains all the information qdbusxml2cpp needs, it's time to generate the Adaptor and Interface classes.

Since custom types are involved, you will need to specify some extra includes, so the Adaptor and Interface classes can find all the type information.

For the DBusChat example, the Adaptor and Interface classes were created by calling

qdbusxml2cpp Chat.xml -i Message.hpp -a ChatAdaptor
qdbusxml2cpp Chat.xml -i Message.hpp -p ChatInterface

Qt Meta object magic

Even though we now have an Adaptor to wrap an object and an Interface to talk to it, you will not be able to compile them as some extra things are necessary for the Qt Meta object system to be able to process the custom type.

Register the type

Declare the type as a Qt meta type by adding a

 Q_DECLARE_METATYPE

statement to the header file containing

your custom type definition.

Add qRegisterMetaType and qDBusRegisterMetaType calls to enable the framework to work with the custom type.

In the example's Message class, a static method is included to do this:

void Message::registerMetaType()
{
    qRegisterMetaType<Message>("Message");

    qDBusRegisterMetaType<Message>();
}
Important
You need to register the type both in the application publishing the object and in the application using it, since both applications need to be able to handle the custom type.

Also take care to register the custom types before calling methods on the Adaptor/Interface that need them.

Qt will show error output if you are using types it cannot (yet) handle, but you should be aware of it nonetheless.

Provide QDBusArgument streaming operators

When a DBus call is executed that uses a custom type, the Qt framework will need to marshal (serialize) or unmarshal (unserialize) the instance.

This is done by using the QDBusArgument stream operators, so you will need to implement those operators for your custom types, as explained here.

For the Message type from the example, these operators are very simple, since it only contains 2 strings:

QDBusArgument &operator<<(QDBusArgument &argument, const Message& message)
{
    argument.beginStructure();
    argument << message.m_user;
    argument << message.m_text;
    argument.endStructure();

    return argument;
}

const QDBusArgument &operator>>(const QDBusArgument &argument, Message &message)
{
    argument.beginStructure();
    argument >> message.m_user;
    argument >> message.m_text;
    argument.endStructure();

    return argument;
}

QDBusArgument provides functions to handle much more complex types as well.

Use the adaptor and interface classes

You are now ready to start using the Adaptor and Interface classes and have your custom type handled automatically.

Publishing an object using an Adaptor

To publish an object, you should instantiate an Adaptor for it and then register the object with the DBus you are using.

This is the code that publishes a Chat object in the DBusChat example:

Chat* pChat = new Chat(&a);
ChatAdaptor* pChatAdaptor = new ChatAdaptor(pChat);

if (!connection.registerService(CHAT_SERVICE))
{
    qFatal("Could not register service!");
}

if (!connection.registerObject(CHAT_PATH, pChat))
{
    qFatal("Could not register Chat object!");
}

The ChatAdaptor instance will process any incoming DBus requests for the Chat object. When a method is invoked, it will call the matching slot on the Chat object. When the Chat object emits a signal, the ChatAdaptor will transmit it across DBus.

Talking to a remote object using an Interface

To talk to a remote object, you need only instantiate the matching Interface and pass the correct service name and object path.

In the DBusChat example, a connection is made with a remote Chat object like this:

demo::Chat chatInterface(CHAT_SERVICE, CHAT_PATH, connection);

Using the remote object in your application

Once you have an instance of the Interface class, you should be able to interact with the remote object like you would with any other QObject.

The ChatWindow class in the DBusChat example, for instance, adds a user simply by calling

m_chatInterface.addUser(m_userName);

Furthermore, ChatWindow is able to respond to signals emitted by the Chat object by connecting to its Interface class just like with any other QObject:

connect(&m_chatInterface, SIGNAL(userAdded(QString)), SLOT(chat_userAdded(QString)));
connect(&m_chatInterface, SIGNAL(userRemoved(QString)), SLOT(chat_userRemoved(QString)));
connect(&m_chatInterface, SIGNAL(messageSent(Message)), SLOT(chat_messageSent(Message)));

Varia

Automation

We're not too wild about having to do some of this stuff manually, but using the Adaptors and Interfaces is much more convenient than writing code that manually constructs dbus calls and processes the replies.

One might hope qdbuscpp2xml and qdbusxml2cpp will be extended to support custom types by analyzing an entire code tree instead of just the header files passed to them, so they would be able to recognize the custom types and add methods/signals/properties to the XML accordingly. One potential strategy for extending qdbuscpp2xml is with plugins, and this is presented at Cpp2XmlPlugins.

Alternatively, a feature could be added to Qt Creator to support the kind of solution presented here in a automated fashion. Since Qt Creator already needs to be aware of custom types used in a project, this might be more feasible (or at least easier) than modifying the qdbus tools.

Adventurous serialization of enumerations

If you happen to use a lot of enumerations and you need them to be exported across DBus, you will likely end up with QDBusArgument stream operators for every enumeration. A basic implementation could simply cast the enum to and from an int, resulting in code looking like this:

QDBusArgument &operator<<(QDBusArgument &argument, const EnumerationType& source)
{
    argument.beginStructure();
    argument << static_cast<int>(source);
    argument.endStructure();
    return argument;
}

const QDBusArgument &operator>>(const QDBusArgument &argument, EnumerationType &dest)
{
    int a;
    argument.beginStructure();
    argument >> a;
    argument.endStructure();

    dest = static_cast<EnumerationType>(a);

    return argument;
}

You will notice that this code will be much the same for all enumerations. The only part that will differ is the "EnumerationType".

Thankfully, C++ knows how to handle templates, so we should be able to write just the one template-based implementation. However, we need that one implementation to handle only enumeration types, not the other custom types we want to send across DBus. Otherwise, any custom type would be cast from and to an integer value, which is probably not what you want for all types, especially not for complex ones.

This is where the boost library comes to our aid. With some magic, it supports conditionals within template definitions.

That way, we can provide QDBusArgument marshaling and unmarshalling implementations that will be used only in situations when such a conditional is true.

This is what the marshaling and unmarshaling code actually looks like:

template<typename T, typename TEnum>
class QDBusEnumMarshal;

template<typename T>
class QDBusEnumMarshal<T, boost::true_type>
{
public:
	static QDBusArgument& marshal(QDBusArgument &argument, const T& source)
	{
		argument.beginStructure();
		argument << static_cast<int>(source);
		argument.endStructure();
		return argument;
	}

	static const QDBusArgument& unmarshal(const QDBusArgument &argument, T &source)
	{
		int a;
		argument.beginStructure();
		argument >> a;
		argument.endStructure();
	
		source = static_cast<T>(a);
	
		return argument;
	}
}

The marshal implementation can now be called by invoking:

QDBusEnumMarshal<T, typename boost::is_enum<T>::type>::marshal(argument, source);

with T being the type we want to marshal.

Some further glue code is needed to link the implementation to the QDbusArgument stream operators:

template<typename T>
QDBusArgument& operator<<(QDBusArgument &argument, const T& source)
{
    return QDBusEnumMarshal<T, typename boost::is_enum<T>::type>::marshal(argument, source);
}

template<typename T>
const QDBusArgument& operator>>(const QDBusArgument &argument, T &source)
{
    return QDBusEnumMarshal<T, typename boost::is_enum<T>::type>::unmarshal(argument, source);
}

Put all that in a header file and include it where you declare the enumerations that need to pass across DBus. The compiler is now able to find the streaming operators all on its own.

You do still need to declare the enumeration as a metatype and register it with the Qt meta object system though.

Note
You only need a boost header file to do this (boost/type_traits/is_enum.hpp, to be exact). There is no need to have the entire library installed, which is rather nice on an embedded platform, since the boost library takes up quite a bit of space.


enumDBus.hpp

This is the entire header file needed to marshal / unmarshal any enumeration:

#ifndef _ENUM_DBUS_HPP
#define _ENUM_DBUS_HPP

#include <QtDBus/QDBusArgument>

#include <boost/type_traits/is_enum.hpp>

using namespace std;

template<typename T, typename TEnum>
class QDBusEnumMarshal;

template<typename T>
class QDBusEnumMarshal<T, boost::true_type>
{
public:
	static QDBusArgument& marshal(QDBusArgument &argument, const T& source)
	{
		argument.beginStructure();
		argument << static_cast<int>(source);
		argument.endStructure();
		return argument;
	}

	static const QDBusArgument& unmarshal(const QDBusArgument &argument, T &source)
	{
		int a;
		argument.beginStructure();
		argument >> a;
		argument.endStructure();
	
		source = static_cast<T>(a);
	
		return argument;
	}
};

template<typename T>
QDBusArgument& operator<<(QDBusArgument &argument, const T& source)
{
	return QDBusEnumMarshal<T, typename boost::is_enum<T>::type>::marshal(argument, source);
}

template<typename T>
const QDBusArgument& operator>>(const QDBusArgument &argument, T &source)
{
	return QDBusEnumMarshal<T, typename boost::is_enum<T>::type>::unmarshal(argument, source);
}

#endif //_ENUM_DBUS_HPP

Example

This section contains the sources for the chat example. The wiki does not allow the upload of files other than images, so they've been posted as text.

Chat is the interface being published. ChatAdaptor and ChatInterface were generated using Chat.xml.

Put all these files in a directory and run

qmake

make

Chat.hpp

#ifndef CHAT_HPP
#define CHAT_HPP

#include <QObject>
#include <QStringList>

class Message;

class Chat : public QObject
{
    Q_OBJECT

    Q_CLASSINFO("D-Bus Interface", "demo.Chat")
    Q_PROPERTY( QStringList users READ users)

signals:
    void userAdded(const QString& user);
    void userRemoved(const QString& user);

    void messageSent(const Message &message);

public slots:
    void addUser(const QString &user);
    void removeUser(const QString &user);

    void sendMessage(const Message &message);

public:
    Chat(QObject* parent = 0);
    virtual ~Chat();

    QStringList users() const;

private:
    QStringList m_users;
};
#endif // CHAT_HPP

Chat.cpp

#include "Chat.hpp"

Chat::Chat(QObject* parent) :
        QObject(parent)
{
}

Chat::~Chat()
{
}

void Chat::addUser(const QString &user)
{
    if (!m_users.contains(user))
    {
        m_users.append(user);

        emit userAdded(user);
    }
}

void Chat::removeUser(const QString &user)
{
    if (m_users.contains(user))
    {
        m_users.removeOne(user);

        emit userRemoved(user);
    }
}

void Chat::sendMessage(const Message &message)
{
    Q_EMIT messageSent(message);
}

QStringList Chat::users() const
{
    return m_users;
}

Chat.xml

<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN" "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
<node>
  <interface name="demo.Chat">
    <property name="users" type="as" access="read"/>
    <signal name="userAdded">
      <arg name="user" type="s" direction="out"/>
    </signal>
    <signal name="userRemoved">
      <arg name="user" type="s" direction="out"/>
    </signal>
    <signal name="messageSent">
      <arg name="message" type="a(ii)" direction="out"/>
      <!-- to support also Qt < 5.6.3 & 5.7.0, use instead "org.qtproject.QtDBus.QtTypeName.In0" -->
      <annotation name="com.trolltech.QtDBus.QtTypeName.Out0" value="Message"/>
    </signal>
    <method name="addUser">
      <arg name="user" type="s" direction="in"/>
    </method>
    <method name="removeUser">
      <arg name="user" type="s" direction="in"/>
    </method>
    <method name="sendMessage">
      <arg name="message" type="a(ii)" direction="in"/>
      <annotation name="com.trolltech.QtDBus.QtTypeName.In0" value="Message"/>
    </method>
  </interface>
</node>

ChatAdaptor.h

/*
 * This file was generated by qdbusxml2cpp version 0.7
 * Command line was: qdbusxml2cpp Chat.xml -i Message.hpp -a ChatAdaptor
 *
 * qdbusxml2cpp is Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
 *
 * This is an auto-generated file.
 * This file may have been hand-edited. Look for HAND-EDIT comments
 * before re-generating it.
 */

#ifndef CHATADAPTOR_H_1270658274
#define CHATADAPTOR_H_1270658274

#include <QtCore/QObject>
#include <QtDBus/QtDBus>
#include "Message.hpp"
class QByteArray;
template<class T> class QList;
template<class Key, class Value> class QMap;
class QString;
class QStringList;
class QVariant;

/*
 * Adaptor class for interface demo.Chat
 */
class ChatAdaptor: public QDBusAbstractAdaptor
{
    Q_OBJECT
    Q_CLASSINFO("D-Bus Interface", "demo.Chat")
    Q_CLASSINFO("D-Bus Introspection", ""
"  <interface name=\"demo.Chat\">\n"
"    <property access=\"read\" type=\"as\" name=\"users\"/>\n"
"    <signal name=\"userAdded\">\n"
"      <arg direction=\"out\" type=\"s\" name=\"user\"/>\n"
"    </signal>\n"
"    <signal name=\"userRemoved\">\n"
"      <arg direction=\"out\" type=\"s\" name=\"user\"/>\n"
"    </signal>\n"
"    <signal name=\"messageSent\">\n"
"      <arg direction=\"out\" type=\"a(ii)\" name=\"message\"/>\n"
"      <!-- to support also Qt < 5.6.3 & 5.7.0, use instead "org.qtproject.QtDBus.QtTypeName.In0" -->\n"
"      <annotation value=\"Message\" name=\"com.trolltech.QtDBus.QtTypeName.Out0\"/>\n"
"    </signal>\n"
"    <method name=\"addUser\">\n"
"      <arg direction=\"in\" type=\"s\" name=\"user\"/>\n"
"    </method>\n"
"    <method name=\"removeUser\">\n"
"      <arg direction=\"in\" type=\"s\" name=\"user\"/>\n"
"    </method>\n"
"    <method name=\"sendMessage\">\n"
"      <arg direction=\"in\" type=\"a(ii)\" name=\"message\"/>\n"
"      <annotation value=\"Message\" name=\"com.trolltech.QtDBus.QtTypeName.In0\"/>\n"
"    </method>\n"
"  </interface>\n"
        "")
public:
    ChatAdaptor(QObject *parent);
    virtual ~ChatAdaptor();

public: // PROPERTIES
    Q_PROPERTY(QStringList users READ users)
    QStringList users() const;

public Q_SLOTS: // METHODS
    void addUser(const QString &user);
    void removeUser(const QString &user);
    void sendMessage(Message message);
Q_SIGNALS: // SIGNALS
    void messageSent(Message message);
    void userAdded(const QString &user);
    void userRemoved(const QString &user);
};

#endif

ChatAdaptor.cpp

/*
 * This file was generated by qdbusxml2cpp version 0.7
 * Command line was: qdbusxml2cpp Chat.xml -i Message.hpp -a ChatAdaptor
 *
 * qdbusxml2cpp is Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
 *
 * This is an auto-generated file.
 * Do not edit! All changes made to it will be lost.
 */

#include "ChatAdaptor.h"
#include <QtCore/QMetaObject>
#include <QtCore/QByteArray>
#include <QtCore/QList>
#include <QtCore/QMap>
#include <QtCore/QString>
#include <QtCore/QStringList>
#include <QtCore/QVariant>

/*
 * Implementation of adaptor class ChatAdaptor
 */

ChatAdaptor::ChatAdaptor(QObject *parent)
    : QDBusAbstractAdaptor(parent)
{
    // constructor
    setAutoRelaySignals(true);
}

ChatAdaptor::~ChatAdaptor()
{
    // destructor
}

QStringList ChatAdaptor::users() const
{
    // get the value of property users
    return qvariant_cast< QStringList >(parent()->property("users"));
}

void ChatAdaptor::addUser(const QString &user)
{
    // handle method call demo.Chat.addUser
    QMetaObject::invokeMethod(parent(), "addUser", Q_ARG(QString, user));
}

void ChatAdaptor::removeUser(const QString &user)
{
    // handle method call demo.Chat.removeUser
    QMetaObject::invokeMethod(parent(), "removeUser", Q_ARG(QString, user));
}

void ChatAdaptor::sendMessage(Message message)
{
    // handle method call demo.Chat.sendMessage
    QMetaObject::invokeMethod(parent(), "sendMessage", Q_ARG(Message, message));
}

ChatInterface.h

/*
 * This file was generated by qdbusxml2cpp version 0.7
 * Command line was: qdbusxml2cpp Chat.xml -i Message.hpp -p ChatInterface
 *
 * qdbusxml2cpp is Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
 *
 * This is an auto-generated file.
 * Do not edit! All changes made to it will be lost.
 */

#ifndef CHATINTERFACE_H_1270658265
#define CHATINTERFACE_H_1270658265

#include <QtCore/QObject>
#include <QtCore/QByteArray>
#include <QtCore/QList>
#include <QtCore/QMap>
#include <QtCore/QString>
#include <QtCore/QStringList>
#include <QtCore/QVariant>
#include <QtDBus/QtDBus>
#include "Message.hpp"

/*
 * Proxy class for interface demo.Chat
 */
class DemoChatInterface: public QDBusAbstractInterface
{
    Q_OBJECT
public:
    static inline const char *staticInterfaceName()
    { return "demo.Chat"; }

public:
    DemoChatInterface(const QString &service, const QString &path, const QDBusConnection &connection, QObject *parent = 0);

    ~DemoChatInterface();

    Q_PROPERTY(QStringList users READ users)
    inline QStringList users() const
    { return qvariant_cast< QStringList >(property("users")); }

public Q_SLOTS: // METHODS
    inline QDBusPendingReply<> addUser(const QString &user)
    {
        QList<QVariant> argumentList;
        argumentList << qVariantFromValue(user);
        return asyncCallWithArgumentList(QLatin1String("addUser"), argumentList);
    }

    inline QDBusPendingReply<> removeUser(const QString &user)
    {
        QList<QVariant> argumentList;
        argumentList << qVariantFromValue(user);
        return asyncCallWithArgumentList(QLatin1String("removeUser"), argumentList);
    }

    inline QDBusPendingReply<> sendMessage(Message message)
    {
        QList<QVariant> argumentList;
        argumentList << qVariantFromValue(message);
        return asyncCallWithArgumentList(QLatin1String("sendMessage"), argumentList);
    }

Q_SIGNALS: // SIGNALS
    void messageSent(Message message);
    void userAdded(const QString &user);
    void userRemoved(const QString &user);
};

namespace demo {
  typedef ::DemoChatInterface Chat;
}
#endif

ChatInterface.cpp

/*
 * This file was generated by qdbusxml2cpp version 0.7
 * Command line was: qdbusxml2cpp Chat.xml -i Message.hpp -p ChatInterface
 *
 * qdbusxml2cpp is Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
 *
 * This is an auto-generated file.
 * This file may have been hand-edited. Look for HAND-EDIT comments
 * before re-generating it.
 */

#include "ChatInterface.h"

/*
 * Implementation of interface class DemoChatInterface
 */

DemoChatInterface::DemoChatInterface(const QString &service, const QString &path, const QDBusConnection &connection, QObject *parent)
    : QDBusAbstractInterface(service, path, staticInterfaceName(), connection, parent)
{
}

DemoChatInterface::~DemoChatInterface()
{
}

ChatWindow.hpp

#ifndef CHATWINDOW_HPP
#define CHATWINDOW_HPP

#include <QtGui/QMainWindow>
#include <QtGui/QStringListModel>

#include "ChatInterface.h"

namespace Ui
{
    class ChatWindow;
}

class QCloseEvent;

class ChatWindow : public QMainWindow
{
    Q_OBJECT

public:
    ChatWindow(demo::Chat& chatInterface, QWidget *parent = 0);
    virtual ~ChatWindow();

protected:
    virtual void closeEvent(QCloseEvent *event);

private:
    ChatWindow(const ChatWindow &other);
    ChatWindow& operator=(const ChatWindow &other);

    Ui::ChatWindow *ui;

    QString m_userName;

    QStringListModel m_users;

    demo::Chat &m_chatInterface;

private slots:
    void sendMessage();

    void chat_userAdded(const QString &user);
    void chat_userRemoved(const QString &user);
    void chat_messageSent(const Message &message);
};

#endif // CHATWINDOW_HPP

ChatWindow.cpp

#include "ChatWindow.hpp"
#include "ui_ChatWindow.h"

#include <QtGui/QCloseEvent>
#include <QtGui/QInputDialog>
#include <QtGui/QMessageBox>

#include "Message.hpp"

ChatWindow::ChatWindow(demo::Chat& chatInterface, QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::ChatWindow),
    m_userName(),
    m_users(),
    m_chatInterface(chatInterface)
{
    ui->setupUi(this);

    QStringList userList = chatInterface.users();
    m_users.setStringList(userList);
    m_users.sort(0);

    connect(&m_chatInterface, SIGNAL(userAdded(QString)), SLOT(chat_userAdded(QString)));
    connect(&m_chatInterface, SIGNAL(userRemoved(QString)), SLOT(chat_userRemoved(QString)));
    connect(&m_chatInterface, SIGNAL(messageSent(Message)), SLOT(chat_messageSent(Message)));

    connect(ui->lneMessage, SIGNAL(returnPressed()), SLOT(sendMessage()));
    connect(ui->btnSend, SIGNAL(released()), SLOT(sendMessage()));

    bool ok;
    m_userName = QInputDialog::getText(this, "Username?", "User name:", QLineEdit::Normal, QDir::home().dirName(), &ok, Qt::Dialog);

    if (!ok)
    {
        QApplication::exit();
        qFatal("Cancelled");
    }

    if (userList.contains(m_userName))
    {
        QMessageBox::critical(this, "Invalid Username", "Username " + m_userName + " is already taken");
        QApplication::exit();
        qFatal("Duplicate username");
    }

    m_chatInterface.addUser(m_userName);

    setWindowTitle("Chat -- " + m_userName);

    ui->lstUsers->setModel(&m_users);
}

ChatWindow::~ChatWindow()
{
    delete ui;
}

void ChatWindow::closeEvent(QCloseEvent *event)
{
    QMainWindow::closeEvent(event);

    if (event->isAccepted())
    {
        m_chatInterface.removeUser(m_userName);
    }
}

void ChatWindow::sendMessage()
{
    Message message(m_userName, ui->lneMessage->text());
    m_chatInterface.sendMessage(message);

    ui->lneMessage->clear();
}

void ChatWindow::chat_userAdded(const QString &user)
{
    QStringList users = m_users.stringList();
    users.append(user);
    users.sort();
    m_users.setStringList(users);
}

void ChatWindow::chat_userRemoved(const QString &user)
{
    QStringList users = m_users.stringList();
    users.removeOne(user);
    m_users.setStringList(users);
}

void ChatWindow::chat_messageSent(const Message &message)
{
    ui->txtChat->appendPlainText(message.getUser() + " : " + message.getText());
}

ChatWindow.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>ChatWindow</class>
 <widget class="QMainWindow" name="ChatWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>600</width>
    <height>400</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>ChatWindow</string>
  </property>
  <widget class="QWidget" name="centralWidget">
   <layout class="QVBoxLayout" name="verticalLayout">
    <item>
     <widget class="QWidget" name="widget_2" native="true">
      <layout class="QHBoxLayout" name="horizontalLayout_2">
       <item>
        <widget class="QListView" name="lstUsers">
         <property name="maximumSize">
          <size>
           <width>150</width>
           <height>16777215</height>
          </size>
         </property>
         <property name="focusPolicy">
          <enum>Qt::NoFocus</enum>
         </property>
         <property name="editTriggers">
          <set>QAbstractItemView::NoEditTriggers</set>
         </property>
         <property name="selectionMode">
          <enum>QAbstractItemView::NoSelection</enum>
         </property>
        </widget>
       </item>
       <item>
        <widget class="QPlainTextEdit" name="txtChat">
         <property name="focusPolicy">
          <enum>Qt::NoFocus</enum>
         </property>
        </widget>
       </item>
      </layout>
     </widget>
    </item>
    <item>
     <widget class="QWidget" name="widget" native="true">
      <layout class="QHBoxLayout" name="horizontalLayout">
       <item>
        <widget class="QLineEdit" name="lneMessage"/>
       </item>
       <item>
        <widget class="QPushButton" name="btnSend">
         <property name="text">
          <string>Send</string>
         </property>
        </widget>
       </item>
      </layout>
     </widget>
    </item>
   </layout>
  </widget>
  <widget class="QMenuBar" name="menuBar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>600</width>
     <height>25</height>
    </rect>
   </property>
  </widget>
  <widget class="QToolBar" name="mainToolBar">
   <attribute name="toolBarArea">
    <enum>TopToolBarArea</enum>
   </attribute>
   <attribute name="toolBarBreak">
    <bool>false</bool>
   </attribute>
  </widget>
  <widget class="QStatusBar" name="statusBar"/>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections/>
</ui>

Message.hpp

#ifndef MESSAGE_HPP
#define MESSAGE_HPP

#include <QtDBus>

class Message
{
public:
    Message();
    Message(const QString& user, const QString &message);
    Message(const Message &other);
    Message& operator=(const Message &other);
    ~Message();

    friend QDBusArgument &operator<<(QDBusArgument &argument, const Message &message);
    friend const QDBusArgument &operator>>(const QDBusArgument &argument, Message &message);

    QString getUser() const;
    QString getText() const;

    //register Message with the Qt type system
    static void registerMetaType();

private:
    QString m_user;
    QString m_text;
};

Q_DECLARE_METATYPE(Message)

#endif // MESSAGE_HPP

Message.cpp

#include "Message.hpp"

Message::Message() :
        m_user(),
        m_text()
{
}

Message::Message(const QString &user, const QString &text) :
        m_user(user),
        m_text(text)
{
}

Message::Message(const Message &other) :
        m_user(other.m_user),
        m_text(other.m_text)
{
}

Message& Message::operator=(const Message &other)
{
    m_user = other.m_user;
    m_text = other.m_text;

    return *this;
}

Message::~Message()
{
}

QString Message::getUser() const
{
    return m_user;
}

QString Message::getText() const
{
    return m_text;
}

void Message::registerMetaType()
{
    qRegisterMetaType<Message>("Message");

    qDBusRegisterMetaType<Message>();
}

QDBusArgument &operator<<(QDBusArgument &argument, const Message& message)
{
    argument.beginStructure();
    argument << message.m_user;
    argument << message.m_text;
    argument.endStructure();

    return argument;
}

const QDBusArgument &operator>>(const QDBusArgument &argument, Message &message)
{
    argument.beginStructure();
    argument >> message.m_user;
    argument >> message.m_text;
    argument.endStructure();

    return argument;
}

main.cpp

#include <QtDBus/QDBusConnection>
#include <QtDBus/QDBusConnectionInterface>
#include <QtGui/QApplication>

#include "Chat.hpp"
#include "ChatAdaptor.h"
#include "ChatInterface.h"
#include "ChatWindow.hpp"
#include "Message.hpp"

#define CHAT_SERVICE "demo.dbus.chat"
#define CHAT_PATH "/chat"

int main(int argc, char *argv[])
{
    /*
      Register the Message type first thing, so Qt knows how to handle
      it before an Adaptor/Interface is even constructed.

      It should be ok to register it further down, as long as no
      Message marshaling or unmarshaling takes place, but this is
      definitely the safest way of doing things.
    */
    Message::registerMetaType();

    QApplication a(argc, argv);
    Chat* pChat = NULL;
    ChatAdaptor* pChatAdaptor = NULL;

    /*
      Create a Chat instance and register it with the session bus only if
      the service isn't already available.
    */
    QDBusConnection connection = QDBusConnection::sessionBus();
    if (!connection.interface()->isServiceRegistered(CHAT_SERVICE))
    {
        pChat = new Chat(&a);
        pChatAdaptor = new ChatAdaptor(pChat);

        if (!connection.registerService(CHAT_SERVICE))
        {
            qFatal("Could not register service!");
        }

        if (!connection.registerObject(CHAT_PATH, pChat))
        {
            qFatal("Could not register Chat object!");
        }
    }

    demo::Chat chatInterface(CHAT_SERVICE, CHAT_PATH, connection);

    ChatWindow w(chatInterface);
    w.show();

    int ret = a.exec();

    //cleanup
    if (pChat)
    {
        delete pChat;
    }
    if (pChatAdaptor)
    {
        delete pChatAdaptor;
    }

    return ret;
}

DBusChat.pro

# -------------------------------------------------
# Project created by QtCreator 2010-04-06T21:08:22
# -------------------------------------------------
QT += dbus
TARGET = DBusChat
TEMPLATE = app
SOURCES += main.cpp \
    ChatWindow.cpp \
    Chat.cpp \
    Message.cpp \
    ChatAdaptor.cpp \
    ChatInterface.cpp
HEADERS += ChatWindow.hpp \
    Chat.hpp \
    Message.hpp \
    ChatAdaptor.h \
    ChatInterface.h
FORMS += ChatWindow.ui