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
|
<!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.12.1 Reference Guide</title>
<link rel="stylesheet" href="_static/classic.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.12.1',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true,
SOURCELINK_SUFFIX: '.txt'
};
</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="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.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" role="navigation" aria-label="related navigation">
<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 class="nav-item nav-item-0"><a href="index.html">PyQt 4.12.1 Reference Guide</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<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
<code class="xref py py-mod docutils literal"><span class="pre">QtDBus</span></code> module provides wrappers for the standard Qt DBus classes.
The <code class="xref py py-mod docutils literal"><span class="pre">dbus.mainloop.qt</span></code> module add support for the Qt event loop to the
standard <code class="docutils literal"><span class="pre">dbus-python</span></code> Python module.</p>
<div class="section" id="qtdbus">
<h2><code class="xref py py-mod docutils literal"><span class="pre">QtDBus</span></code><a class="headerlink" href="#qtdbus" title="Permalink to this headline">¶</a></h2>
<p>The <code class="xref py py-mod docutils literal"><span class="pre">QtDBus</span></code> 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
<code class="docutils literal"><span class="pre">qDBusRegisterMetaType()</span></code> which isn’t possible from Python. Instead a slot
that accepts a DBus structure in an argument should specify a slot with a
single <code class="xref py py-class docutils literal"><span class="pre">QDBusMessage</span></code> argument. The implementation of the
slot should then extract the arguments from the message using its
<code class="xref py py-meth docutils literal"><span class="pre">arguments()</span></code> method.</p>
<p>For example, say we have a DBus method called <code class="docutils literal"><span class="pre">setColors()</span></code> 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 <code class="docutils literal"><span class="pre">a(iii)</span></code>. 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-default"><div class="highlight"><pre><span></span><span class="n">struct</span> <span class="n">Color</span>
<span class="p">{</span>
<span class="nb">int</span> <span class="n">red</span><span class="p">;</span>
<span class="nb">int</span> <span class="n">green</span><span class="p">;</span>
<span class="nb">int</span> <span class="n">blue</span><span class="p">;</span>
<span class="p">};</span>
<span class="n">Q_DECLARE_METATYPE</span><span class="p">(</span><span class="n">Color</span><span class="p">)</span>
<span class="n">qDBusRegisterMetaType</span><span class="o"><</span><span class="n">Color</span><span class="o">></span><span class="p">();</span>
<span class="k">class</span> <span class="nc">ServerAdaptor</span> <span class="p">:</span> <span class="n">public</span> <span class="n">QDBusAbstractAdaptor</span>
<span class="p">{</span>
<span class="n">Q_OBJECT</span>
<span class="n">public</span> <span class="n">slots</span><span class="p">:</span>
<span class="n">void</span> <span class="n">setColors</span><span class="p">(</span><span class="n">QList</span><span class="o"><</span><span class="n">const</span> <span class="n">Color</span> <span class="o">&></span> <span class="n">colors</span><span class="p">);</span>
<span class="p">};</span>
</pre></div>
</div>
<p>The Python version is, of course, much simpler:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><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="c1"># 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="c1"># 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="nb">print</span><span class="p">(</span><span class="s2">"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><code class="xref py py-mod docutils literal"><span class="pre">dbus.mainloop.qt</span></code><a class="headerlink" href="#dbus-mainloop-qt" title="Permalink to this headline">¶</a></h2>
<p>The <code class="xref py py-mod docutils literal"><span class="pre">dbus.mainloop.qt</span></code> module provides support for the Qt event loop to
<code class="docutils literal"><span class="pre">dbus-python</span></code>. The module’s API is almost identical to that of the
<code class="xref py py-mod docutils literal"><span class="pre">dbus.mainloop.glib</span></code> modules that provides support for the GLib event
loop.</p>
<p>The <code class="xref py py-mod docutils literal"><span class="pre">dbus.mainloop.qt</span></code> module contains the following function.</p>
<dl class="function">
<dt id="DBusQtMainLoop">
<code class="descname">DBusQtMainLoop</code><span class="sig-paren">(</span><em>set_as_default=False</em><span class="sig-paren">)</span><a class="headerlink" href="#DBusQtMainLoop" title="Permalink to this definition">¶</a></dt>
<dd><p>Create a <code class="docutils literal"><span class="pre">dbus.mainloop.NativeMainLoop</span></code> 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 <code class="docutils literal"><span class="pre">dbus-python</span></code> language bindings package to be used with PyQt4:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">dbus.mainloop.qt</span> <span class="k">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="kc">True</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<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"><code class="docutils literal"><span class="pre">QtDBus</span></code></a></li>
<li><a class="reference internal" href="#dbus-mainloop-qt"><code class="docutils literal"><span class="pre">dbus.mainloop.qt</span></code></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" role="search">
<h3>Quick search</h3>
<form class="search" action="search.html" method="get">
<div><input type="text" name="q" /></div>
<div><input type="submit" value="Go" /></div>
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<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 class="nav-item nav-item-0"><a href="index.html">PyQt 4.12.1 Reference Guide</a> »</li>
</ul>
</div>
<div class="footer" role="contentinfo">
© Copyright 2016 Riverbank Computing Limited.
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.6.2.
</div>
</body>
</html>
|