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 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
|
<!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>Integrating Python and QML — PyQt 4.9.3 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.9.3',
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.9.3 Reference Guide" href="index.html" />
<link rel="next" title="Integrating Python and JavaScript in QtWebKit" href="webkit.html" />
<link rel="prev" title="Support for QSettings" href="qsettings.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="webkit.html" title="Integrating Python and JavaScript in QtWebKit"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="qsettings.html" title="Support for QSettings"
accesskey="P">previous</a> |</li>
<li><a href="index.html">PyQt 4.9.3 Reference Guide</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body">
<div class="section" id="integrating-python-and-qml">
<h1>Integrating Python and QML<a class="headerlink" href="#integrating-python-and-qml" title="Permalink to this headline">ΒΆ</a></h1>
<p>Qt v4.7 introduced QML as a means of declaratively describing a user interface
and using JavaScript as a scripting language within QML. It is possible to
write complete standalone QML applications, or to combine them with C++.</p>
<p>PyQt supports the integration of Python and QML as far as it can - given the
limitations of the QML implementation.</p>
<p>There are three types of QML application:</p>
<ul class="simple">
<li><em>Pure</em> applications that are written entirely in QML and can be run using the
<strong class="program">qmlviewer</strong> tool provided with Qt. PyQt fully supports this type of
application. The examples provided by <strong class="program">qtdemo.py</strong> under the <em>Qt
Declarative Examples</em> heading are all of this type.</li>
<li>QML allows <tt class="docutils literal"><span class="pre">QObject</span></tt> instances to be used in QML applications. QML code is
able to call an object’s slots and has read and write access to the object’s
properties. QML code can also respond to changes to the value of a property.
PyQt fully supports the use objects created in Python in this way. The
<strong class="program">minehunt.py</strong> example under the <em>Demonstrations</em> heading of
<strong class="program">qtdemo.py</strong> is an example of this type of application.</li>
<li>QML also allows <tt class="docutils literal"><span class="pre">QObject</span></tt> sub-classes to be used in QML applications so
that new instances are created from QML. PyQt does not support this. The
reason is that QML uses information generated at compile time (specifically
it uses <tt class="docutils literal"><span class="pre">QObject::staticMetaObject</span></tt>) rather than information created
dynamically at run time (i.e. <tt class="docutils literal"><span class="pre">QObject::metaObject()</span></tt>). This approach is
fine for a static language like C++ but causes problems for languages such as
Python that allow types to created dynamically. This limitation doesn’t
place any restriction on the functionality of a Python/QML application, it
only places restrictions on how that application is written.</li>
</ul>
<p>QML can implement JavaScript functions and define signals. PyQt allows both of
these to be accessed transparently from Python. The following is a simple
example application that displays the current date and time when the user
clicks on it.</p>
<p>The following is the QML (in the <tt class="file docutils literal"><span class="pre">message.qml</span></tt>) that defines the user
interface:</p>
<div class="highlight-python"><pre>import Qt 4.7
Rectangle {
signal messageRequired;
function updateMessage(text) {
messageText.text = text
}
anchors.fill: parent; color: "black"
Text {
id: messageText
anchors.centerIn: parent; color: "white"
}
MouseArea {
anchors.fill: parent
onClicked: messageRequired()
}
}</pre>
</div>
<p>This defines a signal <tt class="docutils literal"><span class="pre">messageRequired</span></tt> which is emitted when the user clicks
on the mouse area. It also implements a function <tt class="docutils literal"><span class="pre">updateMessage()</span></tt> which
updates the message being displayed.</p>
<p>The following is the Python code that implements the application logic:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">sys</span>
<span class="kn">from</span> <span class="nn">PyQt4.QtCore</span> <span class="kn">import</span> <span class="n">QDateTime</span><span class="p">,</span> <span class="n">QObject</span><span class="p">,</span> <span class="n">QUrl</span><span class="p">,</span> <span class="n">pyqtSignal</span>
<span class="kn">from</span> <span class="nn">PyQt4.QtGui</span> <span class="kn">import</span> <span class="n">QApplication</span>
<span class="kn">from</span> <span class="nn">PyQt4.QtDeclarative</span> <span class="kn">import</span> <span class="n">QDeclarativeView</span>
<span class="c"># This class will emit the current date and time as a signal when</span>
<span class="c"># requested.</span>
<span class="k">class</span> <span class="nc">Now</span><span class="p">(</span><span class="n">QObject</span><span class="p">):</span>
<span class="n">now</span> <span class="o">=</span> <span class="n">pyqtSignal</span><span class="p">(</span><span class="nb">str</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">emit_now</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="n">formatted_date</span> <span class="o">=</span> <span class="n">QDateTime</span><span class="o">.</span><span class="n">currentDateTime</span><span class="p">()</span><span class="o">.</span><span class="n">toString</span><span class="p">()</span>
<span class="bp">self</span><span class="o">.</span><span class="n">now</span><span class="o">.</span><span class="n">emit</span><span class="p">(</span><span class="n">formatted_date</span><span class="p">)</span>
<span class="n">app</span> <span class="o">=</span> <span class="n">QApplication</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">)</span>
<span class="n">now</span> <span class="o">=</span> <span class="n">Now</span><span class="p">()</span>
<span class="c"># Create the QML user interface.</span>
<span class="n">view</span> <span class="o">=</span> <span class="n">QDeclarativeView</span><span class="p">()</span>
<span class="n">view</span><span class="o">.</span><span class="n">setSource</span><span class="p">(</span><span class="n">QUrl</span><span class="p">(</span><span class="s">'message.qml'</span><span class="p">))</span>
<span class="n">view</span><span class="o">.</span><span class="n">setResizeMode</span><span class="p">(</span><span class="n">QDeclarativeView</span><span class="o">.</span><span class="n">SizeRootObjectToView</span><span class="p">)</span>
<span class="c"># Get the root object of the user interface. It defines a</span>
<span class="c"># 'messageRequired' signal and JavaScript 'updateMessage' function. Both</span>
<span class="c"># can be accessed transparently from Python.</span>
<span class="n">rootObject</span> <span class="o">=</span> <span class="n">view</span><span class="o">.</span><span class="n">rootObject</span><span class="p">()</span>
<span class="c"># Provide the current date and time when requested by the user interface.</span>
<span class="n">rootObject</span><span class="o">.</span><span class="n">messageRequired</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="n">now</span><span class="o">.</span><span class="n">emit_now</span><span class="p">)</span>
<span class="c"># Update the user interface with the current date and time.</span>
<span class="n">now</span><span class="o">.</span><span class="n">now</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="n">rootObject</span><span class="o">.</span><span class="n">updateMessage</span><span class="p">)</span>
<span class="c"># Provide an initial message as a prompt.</span>
<span class="n">rootObject</span><span class="o">.</span><span class="n">updateMessage</span><span class="p">(</span><span class="s">"Click to get the current date and time"</span><span class="p">)</span>
<span class="c"># Display the user interface and allow the user to interact with it.</span>
<span class="n">view</span><span class="o">.</span><span class="n">setGeometry</span><span class="p">(</span><span class="mi">100</span><span class="p">,</span> <span class="mi">100</span><span class="p">,</span> <span class="mi">400</span><span class="p">,</span> <span class="mi">240</span><span class="p">)</span>
<span class="n">view</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
<span class="n">app</span><span class="o">.</span><span class="n">exec_</span><span class="p">()</span>
</pre></div>
</div>
<p>Hopefully the comments in the code are sufficient explanation. The important
things to note are that the signal and JavaScript function appear as
appropriate attributes of the root object <tt class="docutils literal"><span class="pre">QDeclarativeItem</span></tt> returned by the
<tt class="docutils literal"><span class="pre">rootObject()</span></tt> method of <tt class="docutils literal"><span class="pre">QDeclarativeView</span></tt> and can be connected to and
called respectively.</p>
</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>
<h4>Previous topic</h4>
<p class="topless"><a href="qsettings.html"
title="previous chapter">Support for QSettings</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="webkit.html"
title="next chapter">Integrating Python and JavaScript in QtWebKit</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="webkit.html" title="Integrating Python and JavaScript in QtWebKit"
>next</a> |</li>
<li class="right" >
<a href="qsettings.html" title="Support for QSettings"
>previous</a> |</li>
<li><a href="index.html">PyQt 4.9.3 Reference Guide</a> »</li>
</ul>
</div>
<div class="footer">
© Copyright 2011 Riverbank Computing Limited.
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3.
</div>
</body>
</html>
|