Re: NetworkManager D-Bus Interface in C++ and Qt

Hello, I want to add NetworkManager D-Bus Interface to my project ( I saw the official specification ( and I made tests, but I can't use the NetworkManager D-Bus Interface in my project. I have the NetworkManager libs installed on Ubuntu 9.04 Jaunty, and I am working in C++ with the Qt libraries. I saw the code of nm-applet and KNetworkManager but they need other extra libraries. Can you put me a code example of how use the NetworkManager D-Bus Interface in C++ with Qt libraries?
Thanks for your attention,
Luis Iv?n Cuende

Luis -

Depending on what you want to do, it can be really easy, or really painful. A good place to start is the qdbusxml2cpp tool that is included with Qt when it is built with support for DBus. Using that tool, you can generate a lot of the code you need by using the introspection files that are included in the network manager source code. Once those classes are generated, they are easy to use. DBus events show up as Qt signals. DBus calls are method calls. All the DBus work happens behind the scenes.

However, you will probably run in to some issues right off the bat with a lot of the introspection files in NetworkManager. The qdbusxml2cpp program is pretty simple minded when it comes to the signatures that it supports. As soon as you hit a signature of any complexity the tool will error out. If you don't need the method that has the complex signature, you can comment it out, and run the tool again.

For the more complex ones, you are going to have to put the code together yourself. Fortunately, making the DBus calls isn't terribly difficult. Building the queries and parsing the responses is a different story.

Unfortunately, it seems to be difficult to find any useful information on the net about how to do what you want to do. I managed to figure some of it out with a significant amount of trial and error.

Here is a bit of code that gets a list of devices from Network Manager using Qt. (The hard way.) I have removed most of the error checking to make the code a little easier to follow -- hope this helps :

QStringList *NetworkManager::getDevices()
   QStringList *myList = NULL;
   QDBusConnection bus = QDBusConnection::systemBus();
QDBusInterface dbus_interface("org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager",
                                 "org.freedesktop.NetworkManager", bus);
   QDBusMessage reply ="GetDevices");

   // Make sure that result contains a reply.
   if (reply.type() == QDBusMessage::ReplyMessage)
       // If it is the reply we expect, it should be a QDBusArgument
       if (reply.arguments().at(0).canConvert<QDBusArgument>())
           // That contains an Array of QStrings holding our Device list.
QDBusArgument myArg = reply.arguments().at(0).value<QDBusArgument>();
           if (myArg.currentType() == QDBusArgument::ArrayType)

               // Get memory for our resulting string list.
               myList = new QStringList();
myList->clear(); // It should already be clear, but just to be safe.

               while (!myArg.atEnd())
                   QString myElement = qdbus_cast<QString>(myArg);
   return myList;

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]