dbus example

Thiago Macieira thiago at kde.org
Tue Aug 1 16:25:22 UTC 2017


On terça-feira, 1 de agosto de 2017 00:53:56 PDT Yubin Ruan wrote:
> 2017-08-01 15:44 GMT+08:00 Thiago Macieira <thiago at kde.org>:
> > Any C++ binding will work? Then I can give you examples using QtDBus.
> 
> Yes. But please offer a workable example. Many thanks!

A very simple client is:

	QDBusInterface iface("org.example.servicename", 
		"/org/example/object/path",
		"org.example.InterfaceName");
	QDBusPendingReply<int> reply = iface.asyncCall("Method", 1, 2, 3);
	reply.waitForFinished();	// blocks until finished
	if (reply.isError()) {
		// handle reply.error()
	} else {
		int value = reply;
		// use value
	}

This is not the way I recommend doing clients, though, since you can get the 
spelling of "Method" wrong or the types of the arguments (three ints in this 
case wrong too. First, note the blocking call to waitForFinished(). If 
possible, you should not block and just use the event loop to wait for the 
reply there -- use QDBusPendingCallWatcher.

Second, I recommend taking your interface definition's XML file and pass it 
through qdbusxml2cpp to create both the interface and the adaptor classes. The 
interface class you'll use in place of QDBusInterface above. It'll have a C++ 
method called "Method" that takes the correct parameter types and returns the 
right QDBusPendingReply type.

The adaptor class is for the server side. See QtDBus documentation and 
examples for more.

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
   Software Architect - Intel Open Source Technology Center



More information about the dbus mailing list