Difference between revisions of "Archive:Development/Tutorials/D-Bus/Intermediate D-Bus (zh CN)"

Jump to: navigation, search
(New page: {{Template:I18n/Language Navigation Bar|Development/Tutorials/D-Bus/Intermediate D-Bus}} {{TutorialBrowser| series=D-Bus| name=中级D-Bus编程| pre=[[../Accessing Interfaces|访问D-...)
 
m (Text replace - "<code cppqt>" to "<syntaxhighlight lang="cpp-qt">")
Line 20: Line 20:
 
Lists of values returned by D-Bus methods are mapped to {{qt|QList}} in QtDBus.  The appropriate specialisation of {{qt|QList}} should be declared as a type to the Qt type system, for example:
 
Lists of values returned by D-Bus methods are mapped to {{qt|QList}} in QtDBus.  The appropriate specialisation of {{qt|QList}} should be declared as a type to the Qt type system, for example:
  
<code cppqt>
+
<syntaxhighlight lang="cpp-qt">
 
Q_DECLARE_METATYPE(QList<QDBusObjectPath>)
 
Q_DECLARE_METATYPE(QList<QDBusObjectPath>)
 
</code>
 
</code>
Line 28: Line 28:
 
The type should also be declared to QtDBus using:
 
The type should also be declared to QtDBus using:
 
      
 
      
<code cppqt>
+
<syntaxhighlight lang="cpp-qt">
 
qDBusRegisterMetaType<QList<QDBusObjectPath> >();
 
qDBusRegisterMetaType<QList<QDBusObjectPath> >();
 
</code>
 
</code>

Revision as of 21:28, 29 June 2011


Contents

Development/Tutorials/D-Bus/Intermediate D-Bus


中级D-Bus编程
Tutorial Series   D-Bus
Previous   访问D-Bus接口
What's Next   创建D-Bus接口
Further Reading   n/a

摘要

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.

复杂返回类型

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 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>)
</code>
 
It is essential that the <tt>Q_DECLARE_METATYPE</tt> 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:
 
<syntaxhighlight lang="cpp-qt">
qDBusRegisterMetaType<QList<QDBusObjectPath> >();
</code>
 
=== Dicts ===
The DBus Dict type should map to {{qt|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 {{qt|QList}}<{{qt|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 = reply.arguments();
int favouriteNumber = values.takeFirst().toInt();
QString favouriteColour = values.takeFirst().toString();
QString favouriteFlavour = values.takeFirst().toString();
</code>
 
== Interfaces that don't support Introspect ==
{{qt|QDBusInterface}}, as a proxy for the remote D-Bus interface, makes use of introspection to provide high level access to D-Bus signals and properties.  However, the object must support the interface <tt>org.freedesktop.DBus.Introspectable</tt> to do so, which is not mandatory.  
 
=== Properties ===
Introspect is required to discover properties that are accessed via QObject::property().  If it is not present, but the names and signature of the properties are known by looking at the source code of the remote interface, the D-Bus property system can be used manually, with these methods:
 
<code>
org.freedesktop.DBus.Properties.Get (in STRING interface_name,
                                     in STRING property_name,
                                     out VARIANT value);
org.freedesktop.DBus.Properties.Set (in STRING interface_name,
                                     in STRING property_name,
                                     in VARIANT value);
</code>
 
=== 信号 ===
If Introspect is not supported, QObject::connect() will get a 'no such signal' error at runtime.
 
It is still possible to connect to these signals with QtDBus, at a lower level, using {{qt|QDBusConnection}}::connect().  If you are using {{qt|QDBusInterface}} for its convenient call() methods, get its connection and call connect() on this:
 
<code cppqt n >
QDBusInterface iface( "org.kde.DBusTute",
                      "/org/kde/DBusTute/Favourites",
                      "org.kde.DBusTute.Favourites",
                      QDBus::sessionBus(), 0 );
 
iface.connection().connect( "org.kde.DBusTute",
                            "/org/kde/DBusTute/Favourites",
                            "org.kde.DBusTute.Favourites",
                            "FavouritesChanged", this, 
                            SLOT(favouritesChanged() ) );
</code>
 
The connection semantics are similar to a regular QObject::connect().

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