1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>DBus Support — PyQt 4.11.2 Reference Guide</title>
<link rel="stylesheet" href="_static/default.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: './',
VERSION: '4.11.2',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<link rel="shortcut icon" href="_static/logo_tn.ico"/>
<link rel="top" title="PyQt 4.11.2 Reference Guide" href="index.html" />
<link rel="next" title="Selecting Incompatible APIs" href="incompatible_apis.html" />
<link rel="prev" title="Internationalisation of PyQt4 Applications" href="i18n.html" />
</head>
<body>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="incompatible_apis.html" title="Selecting Incompatible APIs"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="i18n.html" title="Internationalisation of PyQt4 Applications"
accesskey="P">previous</a> |</li>
<li><a href="index.html">PyQt 4.11.2 Reference Guide</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body">
<div class="section" id="dbus-support">
<span id="ref-dbus"></span><h1>DBus Support<a class="headerlink" href="#dbus-support" title="Permalink to this headline">¶</a></h1>
<p>PyQt4 provides two different modules that implement support for DBus. The
<tt class="xref py py-mod docutils literal"><span class="pre">QtDBus</span></tt> module provides wrappers for the standard Qt DBus classes.
The <tt class="xref py py-mod docutils literal"><span class="pre">dbus.mainloop.qt</span></tt> module add support for the Qt event loop to the
standard <tt class="docutils literal"><span class="pre">dbus-python</span></tt> Python module.</p>
<div class="section" id="qtdbus">
<h2><tt class="xref py py-mod docutils literal"><span class="pre">QtDBus</span></tt><a class="headerlink" href="#qtdbus" title="Permalink to this headline">¶</a></h2>
<p>The <tt class="xref py py-mod docutils literal"><span class="pre">QtDBus</span></tt> module is used in a similar way to the C++ library it
wraps. The main difference is in the way it supports the demarshalling of
DBus structures. C++ relies on the template-based registration of types using
<tt class="docutils literal"><span class="pre">qDBusRegisterMetaType()</span></tt> which isn’t possible from Python. Instead a slot
that accepts a DBus structure in an argument should specify a slot with a
single <tt class="xref py py-class docutils literal"><span class="pre">QDBusMessage</span></tt> argument. The implementation of the
slot should then extract the arguments from the message using its
<tt class="xref py py-meth docutils literal"><span class="pre">arguments()</span></tt> method.</p>
<p>For example, say we have a DBus method called <tt class="docutils literal"><span class="pre">setColors()</span></tt> that has a single
argument that is an array of structures of three integers (red, green and
blue). The DBus signature of the argument would then be <tt class="docutils literal"><span class="pre">a(iii)</span></tt>. In C++
you would typically define a class to hold the red, green and blue values and
so your code would include the following (incomplete) fragments:</p>
<div class="highlight-python"><div class="highlight"><pre>struct Color
{
int red;
int green;
int blue;
};
Q_DECLARE_METATYPE(Color)
qDBusRegisterMetaType<Color>();
class ServerAdaptor : public QDBusAbstractAdaptor
{
Q_OBJECT
public slots:
void setColors(QList<const Color &> colors);
};
</pre></div>
</div>
<p>The Python version is, of course, much simpler:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">ServerAdaptor</span><span class="p">(</span><span class="n">QDBusAbstractAdaptor</span><span class="p">):</span>
<span class="nd">@pyqtSlot</span><span class="p">(</span><span class="n">QDBusMessage</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">setColors</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">message</span><span class="p">):</span>
<span class="c"># Get the single argument.</span>
<span class="n">colors</span> <span class="o">=</span> <span class="n">message</span><span class="o">.</span><span class="n">arguments</span><span class="p">()[</span><span class="mi">0</span><span class="p">]</span>
<span class="c"># The argument will be a list of 3-tuples of ints.</span>
<span class="k">for</span> <span class="n">red</span><span class="p">,</span> <span class="n">green</span><span class="p">,</span> <span class="n">blue</span> <span class="ow">in</span> <span class="n">colors</span><span class="p">:</span>
<span class="k">print</span><span class="p">(</span><span class="s">"RGB:"</span><span class="p">,</span> <span class="n">red</span><span class="p">,</span> <span class="n">green</span><span class="p">,</span> <span class="n">blue</span><span class="p">)</span>
</pre></div>
</div>
<p>Note that this technique can be used for arguments of any type, it is only
require if DBus structures are involved.</p>
</div>
<div class="section" id="dbus-mainloop-qt">
<h2><tt class="xref py py-mod docutils literal"><span class="pre">dbus.mainloop.qt</span></tt><a class="headerlink" href="#dbus-mainloop-qt" title="Permalink to this headline">¶</a></h2>
<p>The <tt class="xref py py-mod docutils literal"><span class="pre">dbus.mainloop.qt</span></tt> module provides support for the Qt event loop to
<tt class="docutils literal"><span class="pre">dbus-python</span></tt>. The module’s API is almost identical to that of the
<tt class="xref py py-mod docutils literal"><span class="pre">dbus.mainloop.glib</span></tt> modules that provides support for the GLib event
loop.</p>
<p>The <tt class="xref py py-mod docutils literal"><span class="pre">dbus.mainloop.qt</span></tt> module contains the following function.</p>
<dl class="function">
<dt id="DBusQtMainLoop">
<tt class="descname">DBusQtMainLoop</tt><big>(</big><em>set_as_default=False</em><big>)</big><a class="headerlink" href="#DBusQtMainLoop" title="Permalink to this definition">¶</a></dt>
<dd><p>Create a <tt class="docutils literal"><span class="pre">dbus.mainloop.NativeMainLoop</span></tt> object that uses the the Qt event
loop.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>set_as_default</strong> – is optionally set to make the main loop instance the default for all
new Connection and Bus instances. It may only be specified as a
keyword argument, and not as a positional argument.</td>
</tr>
</tbody>
</table>
</dd></dl>
<p>The following code fragment is all that is normally needed to set up the
standard <tt class="docutils literal"><span class="pre">dbus-python</span></tt> language bindings package to be used with PyQt4:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">dbus.mainloop.qt</span> <span class="kn">import</span> <span class="n">DBusQtMainLoop</span>
<span class="n">DBusQtMainLoop</span><span class="p">(</span><span class="n">set_as_default</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar">
<div class="sphinxsidebarwrapper">
<p class="logo"><a href="index.html">
<img class="logo" src="_static/logo.png" alt="Logo"/>
</a></p>
<h3><a href="index.html">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">DBus Support</a><ul>
<li><a class="reference internal" href="#qtdbus"><tt class="docutils literal"><span class="pre">QtDBus</span></tt></a></li>
<li><a class="reference internal" href="#dbus-mainloop-qt"><tt class="docutils literal"><span class="pre">dbus.mainloop.qt</span></tt></a></li>
</ul>
</li>
</ul>
<h4>Previous topic</h4>
<p class="topless"><a href="i18n.html"
title="previous chapter">Internationalisation of PyQt4 Applications</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="incompatible_apis.html"
title="next chapter">Selecting Incompatible APIs</a></p>
<div id="searchbox" style="display: none">
<h3>Quick search</h3>
<form class="search" action="search.html" method="get">
<input type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
<p class="searchtip" style="font-size: 90%">
Enter search terms or a module, class or function name.
</p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="incompatible_apis.html" title="Selecting Incompatible APIs"
>next</a> |</li>
<li class="right" >
<a href="i18n.html" title="Internationalisation of PyQt4 Applications"
>previous</a> |</li>
<li><a href="index.html">PyQt 4.11.2 Reference Guide</a> »</li>
</ul>
</div>
<div class="footer">
© Copyright 2014 Riverbank Computing Limited.
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.2.2.
</div>
</body>
</html>
|