File: gotchas.html

package info (click to toggle)
python-qt4 4.9.3-4
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 34,432 kB
  • sloc: python: 34,126; cpp: 11,938; xml: 290; makefile: 223; php: 27
file content (257 lines) | stat: -rw-r--r-- 19,294 bytes parent folder | download
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257


<!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>Things to be Aware Of &mdash; 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="Using Qt Designer" href="designer.html" />
    <link rel="prev" title="Old-style Signal and Slot Support" href="old_style_signals_slots.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="designer.html" title="Using Qt Designer"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="old_style_signals_slots.html" title="Old-style Signal and Slot Support"
             accesskey="P">previous</a> |</li>
        <li><a href="index.html">PyQt 4.9.3 Reference Guide</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="things-to-be-aware-of">
<h1>Things to be Aware Of<a class="headerlink" href="#things-to-be-aware-of" title="Permalink to this headline">¶</a></h1>
<div class="section" id="python-strings-qt-strings-and-unicode">
<h2>Python Strings, Qt Strings and Unicode<a class="headerlink" href="#python-strings-qt-strings-and-unicode" title="Permalink to this headline">¶</a></h2>
<p>PyQt uses the <tt class="docutils literal"><span class="pre">QString</span></tt> class to represent Unicode strings, and the
<tt class="docutils literal"><span class="pre">QByteArray</span></tt> to represent byte arrays or strings.  In Python v3 the
corresponding native object types are <tt class="docutils literal"><span class="pre">str</span></tt> and <tt class="docutils literal"><span class="pre">bytes</span></tt>.  In Python v2 the
corresponding native object types are <tt class="docutils literal"><span class="pre">unicode</span></tt> and <tt class="docutils literal"><span class="pre">str</span></tt>.</p>
<p>PyQt does its best to automatically convert between objects of the various
types.  Explicit conversions can be easily made where necessary.</p>
<p>In some cases PyQt will not perform automatic conversions where it is
necessary to distinguish between different overloaded methods.</p>
<p>For Python v3 the following conversions are done by default.</p>
<ul class="simple">
<li>If Qt expects a <tt class="docutils literal"><span class="pre">char</span> <span class="pre">*</span></tt> (or a <tt class="docutils literal"><span class="pre">const</span></tt> version) then PyQt will accept a
<tt class="docutils literal"><span class="pre">str</span></tt> or <tt class="docutils literal"><span class="pre">QString</span></tt> that contains only ASCII characters, a <tt class="docutils literal"><span class="pre">bytes</span></tt>, a
<tt class="docutils literal"><span class="pre">QByteArray</span></tt>, or a Python object that implements the buffer protocol.</li>
<li>If Qt expects a <tt class="docutils literal"><span class="pre">char</span></tt> (or a <tt class="docutils literal"><span class="pre">const</span></tt> version) then PyQt will accept the
same types as for <tt class="docutils literal"><span class="pre">char</span> <span class="pre">*</span></tt> and also require that a single character is
provided.</li>
<li>If Qt expects a <tt class="docutils literal"><span class="pre">signed</span> <span class="pre">char</span> <span class="pre">*</span></tt> or an <tt class="docutils literal"><span class="pre">unsigned</span> <span class="pre">char</span> <span class="pre">*</span></tt> (or a <tt class="docutils literal"><span class="pre">const</span></tt>
version) then PyQt will accept a <tt class="docutils literal"><span class="pre">bytes</span></tt>.</li>
<li>If Qt expects a <tt class="docutils literal"><span class="pre">signed</span> <span class="pre">char</span></tt> or an <tt class="docutils literal"><span class="pre">unsigned</span> <span class="pre">char</span></tt> (or a <tt class="docutils literal"><span class="pre">const</span></tt>
version) then PyQt will accept a <tt class="docutils literal"><span class="pre">bytes</span></tt> of length 1.</li>
<li>If Qt expects a <tt class="docutils literal"><span class="pre">QString</span></tt> then PyQt will accept a <tt class="docutils literal"><span class="pre">str</span></tt>, a <tt class="docutils literal"><span class="pre">bytes</span></tt> that
contains only ASCII characters, a <tt class="docutils literal"><span class="pre">QChar</span></tt> or a <tt class="docutils literal"><span class="pre">QByteArray</span></tt>.</li>
<li>If Qt expects a <tt class="docutils literal"><span class="pre">QByteArray</span></tt> then PyQt will also accept a <tt class="docutils literal"><span class="pre">str</span></tt> that
contains only Latin-1 characters, or a <tt class="docutils literal"><span class="pre">bytes</span></tt>.</li>
</ul>
<p>For Python v2 the following conversions are done by default.</p>
<ul class="simple">
<li>If Qt expects a <tt class="docutils literal"><span class="pre">char</span> <span class="pre">*</span></tt>, <tt class="docutils literal"><span class="pre">signed</span> <span class="pre">char</span> <span class="pre">*</span></tt> or an <tt class="docutils literal"><span class="pre">unsigned</span> <span class="pre">char</span> <span class="pre">*</span></tt> (or a
<tt class="docutils literal"><span class="pre">const</span></tt> version) then PyQt will accept a <tt class="docutils literal"><span class="pre">unicode</span></tt> or <tt class="docutils literal"><span class="pre">QString</span></tt> that
contains only ASCII characters, a <tt class="docutils literal"><span class="pre">str</span></tt>, a <tt class="docutils literal"><span class="pre">QByteArray</span></tt>, or a Python
object that implements the buffer protocol.</li>
<li>If Qt expects a <tt class="docutils literal"><span class="pre">char</span></tt>, <tt class="docutils literal"><span class="pre">signed</span> <span class="pre">char</span></tt> or an <tt class="docutils literal"><span class="pre">unsigned</span> <span class="pre">char</span></tt> (or a
<tt class="docutils literal"><span class="pre">const</span></tt> version) then PyQt will accept the same types as for <tt class="docutils literal"><span class="pre">char</span> <span class="pre">*</span></tt>,
<tt class="docutils literal"><span class="pre">signed</span> <span class="pre">char</span> <span class="pre">*</span></tt> and <tt class="docutils literal"><span class="pre">unsigned</span> <span class="pre">char</span> <span class="pre">*</span></tt> and also require that a single
character is provided.</li>
<li>If Qt expects a <tt class="docutils literal"><span class="pre">QString</span></tt> then PyQt will accept a <tt class="docutils literal"><span class="pre">unicode</span></tt>, a <tt class="docutils literal"><span class="pre">str</span></tt>
that contains only ASCII characters, a <tt class="docutils literal"><span class="pre">QChar</span></tt> or a <tt class="docutils literal"><span class="pre">QByteArray</span></tt>.</li>
<li>If Qt expects a <tt class="docutils literal"><span class="pre">QByteArray</span></tt> then PyQt will accept a <tt class="docutils literal"><span class="pre">unicode</span></tt> that
contains only Latin-1 characters, or a <tt class="docutils literal"><span class="pre">str</span></tt>.</li>
</ul>
<p>Note that the different behaviour between Python v2 and v3 is due to v3&#8217;s
reduced support for the buffer protocol.</p>
</div>
<div class="section" id="garbage-collection">
<h2>Garbage Collection<a class="headerlink" href="#garbage-collection" title="Permalink to this headline">¶</a></h2>
<p>C++ does not garbage collect unreferenced class instances, whereas Python does.
In the following C++ fragment both colours exist even though the first can no
longer be referenced from within the program:</p>
<div class="highlight-python"><pre>col = new QColor();
col = new QColor();</pre>
</div>
<p>In the corresponding Python fragment, the first colour is destroyed when the
second is assigned to <tt class="docutils literal"><span class="pre">col</span></tt>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">col</span> <span class="o">=</span> <span class="n">QtGui</span><span class="o">.</span><span class="n">QColor</span><span class="p">()</span>
<span class="n">col</span> <span class="o">=</span> <span class="n">QtGui</span><span class="o">.</span><span class="n">QColor</span><span class="p">()</span>
</pre></div>
</div>
<p>In Python, each colour must be assigned to different names.  Typically this is
done within class definitions, so the code fragment would be something like:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="bp">self</span><span class="o">.</span><span class="n">col1</span> <span class="o">=</span> <span class="n">QtGui</span><span class="o">.</span><span class="n">QColor</span><span class="p">()</span>
<span class="bp">self</span><span class="o">.</span><span class="n">col2</span> <span class="o">=</span> <span class="n">QtGui</span><span class="o">.</span><span class="n">QColor</span><span class="p">()</span>
</pre></div>
</div>
<p>Sometimes a Qt class instance will maintain a pointer to another instance and
will eventually call the destructor of that second instance.  The most common
example is that a <tt class="docutils literal"><span class="pre">QObject</span></tt> (and any of its sub-classes) keeps pointers to
its children and will automatically call their destructors.  In these cases,
the corresponding Python object will also keep a reference to the corresponding
child objects.</p>
<p>So, in the following Python fragment, the first <tt class="docutils literal"><span class="pre">QLabel</span></tt> is not destroyed
when the second is assigned to <tt class="docutils literal"><span class="pre">lab</span></tt> because the parent <tt class="docutils literal"><span class="pre">QWidget</span></tt> still has
a reference to it:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">parent</span> <span class="o">=</span> <span class="n">QtGui</span><span class="o">.</span><span class="n">QWidget</span><span class="p">()</span>
<span class="n">lab</span> <span class="o">=</span> <span class="n">QtGui</span><span class="o">.</span><span class="n">QLabel</span><span class="p">(</span><span class="s">&quot;First label&quot;</span><span class="p">,</span> <span class="n">parent</span><span class="p">)</span>
<span class="n">lab</span> <span class="o">=</span> <span class="n">QtGui</span><span class="o">.</span><span class="n">QLabel</span><span class="p">(</span><span class="s">&quot;Second label&quot;</span><span class="p">,</span> <span class="n">parent</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="multiple-inheritance">
<h2>Multiple Inheritance<a class="headerlink" href="#multiple-inheritance" title="Permalink to this headline">¶</a></h2>
<p>It is not possible to define a new Python class that sub-classes from more than
one Qt class.</p>
</div>
<div class="section" id="access-to-protected-member-functions">
<h2>Access to Protected Member Functions<a class="headerlink" href="#access-to-protected-member-functions" title="Permalink to this headline">¶</a></h2>
<p>When an instance of a C++ class is not created from Python it is not possible
to access the protected member functions, or emit any signals, of that
instance.  Attempts to do so will raise a Python exception.  Also, any Python
methods corresponding to the instance&#8217;s virtual member functions will never be
called.</p>
</div>
<div class="section" id="none-and-null">
<h2><tt class="docutils literal"><span class="pre">None</span></tt> and <tt class="docutils literal"><span class="pre">NULL</span></tt><a class="headerlink" href="#none-and-null" title="Permalink to this headline">¶</a></h2>
<p>Throughout PyQt, the <tt class="docutils literal"><span class="pre">None</span></tt> value can be specified wherever <tt class="docutils literal"><span class="pre">NULL</span></tt> is
acceptable to the underlying C++ code.</p>
<p>Equally, <tt class="docutils literal"><span class="pre">NULL</span></tt> is converted to <tt class="docutils literal"><span class="pre">None</span></tt> whenever it is returned by the
underlying C++ code.</p>
</div>
<div class="section" id="support-for-void">
<h2>Support for <tt class="docutils literal"><span class="pre">void</span> <span class="pre">*</span></tt><a class="headerlink" href="#support-for-void" title="Permalink to this headline">¶</a></h2>
<p>PyQt (actually SIP) represents <tt class="docutils literal"><span class="pre">void</span> <span class="pre">*</span></tt> values as objects of type
<tt class="docutils literal"><span class="pre">sip.voidptr</span></tt>.  Such values are often used to pass the addresses of external
objects between different Python modules.  To make this easier, a Python
integer (or anything that Python can convert to an integer) can be used
whenever a <tt class="docutils literal"><span class="pre">sip.voidptr</span></tt> is expected.</p>
<p>A <tt class="docutils literal"><span class="pre">sip.voidptr</span></tt> may be converted to a Python integer by using the <tt class="docutils literal"><span class="pre">int()</span></tt>
builtin function.</p>
<p>A <tt class="docutils literal"><span class="pre">sip.voidptr</span></tt> may be converted to a Python string by using its
<tt class="docutils literal"><span class="pre">asstring()</span></tt> method.  The <tt class="docutils literal"><span class="pre">asstring()</span></tt> method takes an optional integer
argument which is the length of the data in bytes.</p>
<p>A <tt class="docutils literal"><span class="pre">sip.voidptr</span></tt> may also be given a size (ie. the size of the block of
memory that is pointed to) by calling its <tt class="docutils literal"><span class="pre">setsize()</span></tt> method.  If it has a
size then it is also able to support Python&#8217;s buffer protocol and behaves just
like a Python <tt class="docutils literal"><span class="pre">memoryview</span></tt> object so that the block of memory can be treated
as a mutable list of bytes.  It also means that the Python <tt class="xref py py-mod docutils literal"><span class="pre">struct</span></tt> module
can be used to unpack and pack binary data structures in memory, memory mapped
files or shared memory.</p>
</div>
<div class="section" id="super-and-pyqt-classes">
<h2><tt class="docutils literal"><span class="pre">super</span></tt> and PyQt Classes<a class="headerlink" href="#super-and-pyqt-classes" title="Permalink to this headline">¶</a></h2>
<p>In versions of PyQt earlier than v4.5 there were restrictions on the use of
<tt class="docutils literal"><span class="pre">super</span></tt> with PyQt classes.  These restrictions no longer apply with v4.5 and
later.</p>
</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="#">Things to be Aware Of</a><ul>
<li><a class="reference internal" href="#python-strings-qt-strings-and-unicode">Python Strings, Qt Strings and Unicode</a></li>
<li><a class="reference internal" href="#garbage-collection">Garbage Collection</a></li>
<li><a class="reference internal" href="#multiple-inheritance">Multiple Inheritance</a></li>
<li><a class="reference internal" href="#access-to-protected-member-functions">Access to Protected Member Functions</a></li>
<li><a class="reference internal" href="#none-and-null"><tt class="docutils literal"><span class="pre">None</span></tt> and <tt class="docutils literal"><span class="pre">NULL</span></tt></a></li>
<li><a class="reference internal" href="#support-for-void">Support for <tt class="docutils literal"><span class="pre">void</span> <span class="pre">*</span></tt></a></li>
<li><a class="reference internal" href="#super-and-pyqt-classes"><tt class="docutils literal"><span class="pre">super</span></tt> and PyQt Classes</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="old_style_signals_slots.html"
                        title="previous chapter">Old-style Signal and Slot Support</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="designer.html"
                        title="next chapter">Using Qt Designer</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="designer.html" title="Using Qt Designer"
             >next</a> |</li>
        <li class="right" >
          <a href="old_style_signals_slots.html" title="Old-style Signal and Slot Support"
             >previous</a> |</li>
        <li><a href="index.html">PyQt 4.9.3 Reference Guide</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2011 Riverbank Computing Limited.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3.
    </div>
  </body>
</html>