Qt Development‎ > ‎

Create D-Bus Interface

This is a "quickie" on how to prepare one application to receive D-Bus messages. Make sure to scroll to the bottom of this page and you will find links to "real" tutorials from the nokia web site.

How To

1. Create a xml file (com.crochik.Example.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="com.crochik.Example">
        <method name="sendSMS">
                <arg direction="in" type="s"/>
                <arg direction="out" type="b"/>
        </method>
</interface>
</node>


2. Execute on the dos prompt (windows) or console (linux):

qdbusxml2cpp -c ExampleAdptr -a exampleadptr.h:exampleadptr.cpp com.crochik.Example.xml

You should get two files (exampleadptr.h and exampleadptr.cpp).


3. Add the two files to your project.
4. Include on the .pro file
QT += dbus

5. Change the class that should handle the signal. One suggestion is to create a class that extends QApplication and instantiate it to start the application.

On the .h file add a slot to handle the call (it should match the signature on the adptr class)

// ...

public Q_SLOTS: // METHODS

    bool sendSMS(const QString &in0);

// ...


6. On the .cpp file for the same class include exampleadptr.h and implement the method

        
// ....
#include "exampleadptr.h"
// ...

bool MyClassName::sendSMS(const QString &in0) {

    // do something
}

// ...


7. On the constructor for this class
// ....

MyClassName::MyClassName() {
    // ...
    new ExampleAdptr(this);
    // ....

    registerService();
}


8. Before the application can start receiving dbus signals  you must register the service


bool MyClassName::registerService() {
    QDBusConnection connection = QDBusConnection::sessionBus();
     if ( !connection.registerService("com.crochik.Example") ) {
        qDebug() << "error registering service.";
         return false;
     }
     if ( !connection.registerObject("/", this) ) {
         qDebug() << "error registering object";
         return false;
     }

    
return true;

}


9. You will need to create a "service file" if you want the system to start your application automatically when a dbus signal is sent to it. Something like this:

# Service description file
[D-BUS Service]
Name=com.crochik.Example
Exec=/path/to/yourapp


Links to the "real" tutorials:


ċ
com.crochik.Example.xml
(0k)
Felipe Crochik,
Jun 23, 2010, 4:15 PM
ċ
exampleadptr.cpp
(1k)
Felipe Crochik,
Jun 23, 2010, 4:15 PM
ċ
exampleadptr.h
(1k)
Felipe Crochik,
Jun 23, 2010, 4:15 PM
Comments