File: gotchas.html

package info (click to toggle)
python-qt4 4.12.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 40,300 kB
  • ctags: 6,185
  • sloc: python: 125,988; cpp: 13,291; xml: 292; makefile: 246; php: 27; sh: 2
file content (255 lines) | stat: -rw-r--r-- 20,184 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

<!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 &#8212; 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="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" 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="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 class="nav-item nav-item-0"><a href="index.html">PyQt 4.12.1 Reference Guide</a> &#187;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <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>PyQt4 uses the <code class="docutils literal"><span class="pre">QString</span></code> class to represent Unicode strings, and the
<code class="docutils literal"><span class="pre">QByteArray</span></code> to represent byte arrays or strings.  In Python v3 the
corresponding native object types are <code class="docutils literal"><span class="pre">str</span></code> and <code class="docutils literal"><span class="pre">bytes</span></code>.  In Python v2 the
corresponding native object types are <code class="docutils literal"><span class="pre">unicode</span></code> and <code class="docutils literal"><span class="pre">str</span></code>.</p>
<p>PyQt4 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 PyQt4 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 <code class="docutils literal"><span class="pre">char</span> <span class="pre">*</span></code> (or a <code class="docutils literal"><span class="pre">const</span></code> version) then PyQt4 will accept a
<code class="docutils literal"><span class="pre">str</span></code> or <code class="docutils literal"><span class="pre">QString</span></code> that contains only ASCII characters, a <code class="docutils literal"><span class="pre">bytes</span></code>, a
<code class="docutils literal"><span class="pre">QByteArray</span></code>, or a Python object that implements the buffer protocol.</li>
<li>If Qt expects a <code class="docutils literal"><span class="pre">char</span></code> (or a <code class="docutils literal"><span class="pre">const</span></code> version) then PyQt4 will accept the
same types as for <code class="docutils literal"><span class="pre">char</span> <span class="pre">*</span></code> and also require that a single character is
provided.</li>
<li>If Qt expects a <code class="docutils literal"><span class="pre">signed</span> <span class="pre">char</span> <span class="pre">*</span></code> or an <code class="docutils literal"><span class="pre">unsigned</span> <span class="pre">char</span> <span class="pre">*</span></code> (or a <code class="docutils literal"><span class="pre">const</span></code>
version) then PyQt4 will accept a <code class="docutils literal"><span class="pre">bytes</span></code>.</li>
<li>If Qt expects a <code class="docutils literal"><span class="pre">signed</span> <span class="pre">char</span></code> or an <code class="docutils literal"><span class="pre">unsigned</span> <span class="pre">char</span></code> (or a <code class="docutils literal"><span class="pre">const</span></code>
version) then PyQt4 will accept a <code class="docutils literal"><span class="pre">bytes</span></code> of length 1.</li>
<li>If Qt expects a <code class="docutils literal"><span class="pre">QString</span></code> then PyQt4 will accept a <code class="docutils literal"><span class="pre">str</span></code>, a <code class="docutils literal"><span class="pre">bytes</span></code>
that contains only ASCII characters, a <code class="docutils literal"><span class="pre">QChar</span></code> or a <code class="docutils literal"><span class="pre">QByteArray</span></code>.</li>
<li>If Qt expects a <code class="docutils literal"><span class="pre">QByteArray</span></code> then PyQt4 will also accept a <code class="docutils literal"><span class="pre">str</span></code> that
contains only Latin-1 characters, or a <code class="docutils literal"><span class="pre">bytes</span></code>.</li>
</ul>
<p>For Python v2 the following conversions are done by default.</p>
<ul class="simple">
<li>If Qt expects a <code class="docutils literal"><span class="pre">char</span> <span class="pre">*</span></code>, <code class="docutils literal"><span class="pre">signed</span> <span class="pre">char</span> <span class="pre">*</span></code> or an <code class="docutils literal"><span class="pre">unsigned</span> <span class="pre">char</span> <span class="pre">*</span></code> (or a
<code class="docutils literal"><span class="pre">const</span></code> version) then PyQt4 will accept a <code class="docutils literal"><span class="pre">unicode</span></code> or <code class="docutils literal"><span class="pre">QString</span></code> that
contains only ASCII characters, a <code class="docutils literal"><span class="pre">str</span></code>, a <code class="docutils literal"><span class="pre">QByteArray</span></code>, or a Python
object that implements the buffer protocol.</li>
<li>If Qt expects a <code class="docutils literal"><span class="pre">char</span></code>, <code class="docutils literal"><span class="pre">signed</span> <span class="pre">char</span></code> or an <code class="docutils literal"><span class="pre">unsigned</span> <span class="pre">char</span></code> (or a
<code class="docutils literal"><span class="pre">const</span></code> version) then PyQt4 will accept the same types as for <code class="docutils literal"><span class="pre">char</span> <span class="pre">*</span></code>,
<code class="docutils literal"><span class="pre">signed</span> <span class="pre">char</span> <span class="pre">*</span></code> and <code class="docutils literal"><span class="pre">unsigned</span> <span class="pre">char</span> <span class="pre">*</span></code> and also require that a single
character is provided.</li>
<li>If Qt expects a <code class="docutils literal"><span class="pre">QString</span></code> then PyQt4 will accept a <code class="docutils literal"><span class="pre">unicode</span></code>, a <code class="docutils literal"><span class="pre">str</span></code>
that contains only ASCII characters, a <code class="docutils literal"><span class="pre">QChar</span></code> or a <code class="docutils literal"><span class="pre">QByteArray</span></code>.</li>
<li>If Qt expects a <code class="docutils literal"><span class="pre">QByteArray</span></code> then PyQt4 will accept a <code class="docutils literal"><span class="pre">unicode</span></code> that
contains only Latin-1 characters, or a <code class="docutils literal"><span class="pre">str</span></code>.</li>
</ul>
<p>Note that the different behaviour between Python v2 and v3 is due to v3’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-default"><div class="highlight"><pre><span></span><span class="n">col</span> <span class="o">=</span> <span class="n">new</span> <span class="n">QColor</span><span class="p">();</span>
<span class="n">col</span> <span class="o">=</span> <span class="n">new</span> <span class="n">QColor</span><span class="p">();</span>
</pre></div>
</div>
<p>In the corresponding Python fragment, the first colour is destroyed when the
second is assigned to <code class="docutils literal"><span class="pre">col</span></code>:</p>
<div class="highlight-default"><div class="highlight"><pre><span></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>
<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-default"><div class="highlight"><pre><span></span><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 <code class="docutils literal"><span class="pre">QObject</span></code> (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 <code class="docutils literal"><span class="pre">QLabel</span></code> is not destroyed
when the second is assigned to <code class="docutils literal"><span class="pre">lab</span></code> because the parent <code class="docutils literal"><span class="pre">QWidget</span></code> still has
a reference to it:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><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="s2">&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="s2">&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’s virtual member functions will never be
called.</p>
</div>
<div class="section" id="none-and-null">
<h2><code class="docutils literal"><span class="pre">None</span></code> and <code class="docutils literal"><span class="pre">NULL</span></code><a class="headerlink" href="#none-and-null" title="Permalink to this headline">¶</a></h2>
<p>Throughout PyQt4, the <code class="docutils literal"><span class="pre">None</span></code> value can be specified wherever <code class="docutils literal"><span class="pre">NULL</span></code> is
acceptable to the underlying C++ code.</p>
<p>Equally, <code class="docutils literal"><span class="pre">NULL</span></code> is converted to <code class="docutils literal"><span class="pre">None</span></code> whenever it is returned by the
underlying C++ code.</p>
</div>
<div class="section" id="support-for-void">
<h2>Support for <code class="docutils literal"><span class="pre">void</span> <span class="pre">*</span></code><a class="headerlink" href="#support-for-void" title="Permalink to this headline">¶</a></h2>
<p>PyQt4 (actually SIP) represents <code class="docutils literal"><span class="pre">void</span> <span class="pre">*</span></code> values as objects of type
<code class="docutils literal"><span class="pre">sip.voidptr</span></code>.  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 <code class="docutils literal"><span class="pre">sip.voidptr</span></code> is expected.</p>
<p>A <code class="docutils literal"><span class="pre">sip.voidptr</span></code> may be converted to a Python integer by using the <code class="docutils literal"><span class="pre">int()</span></code>
builtin function.</p>
<p>A <code class="docutils literal"><span class="pre">sip.voidptr</span></code> may be converted to a Python string by using its
<code class="docutils literal"><span class="pre">asstring()</span></code> method.  The <code class="docutils literal"><span class="pre">asstring()</span></code> method takes an optional integer
argument which is the length of the data in bytes.</p>
<p>A <code class="docutils literal"><span class="pre">sip.voidptr</span></code> may also be given a size (ie. the size of the block of
memory that is pointed to) by calling its <code class="docutils literal"><span class="pre">setsize()</span></code> method.  If it has a
size then it is also able to support Python’s buffer protocol and behaves like
a Python <code class="docutils literal"><span class="pre">memoryview</span></code> object so that the block of memory can be treated as a
mutable list of bytes.  It also means that the Python <code class="xref py py-mod docutils literal"><span class="pre">struct</span></code> 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-pyqt4-classes">
<h2><code class="docutils literal"><span class="pre">super</span></code> and PyQt4 Classes<a class="headerlink" href="#super-and-pyqt4-classes" title="Permalink to this headline">¶</a></h2>
<p>In versions of PyQt4 earlier than v4.5 there were restrictions on the use of
<code class="docutils literal"><span class="pre">super</span></code> with PyQt4 classes.  These restrictions no longer apply with v4.5 and
later.</p>
</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="#">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"><code class="docutils literal"><span class="pre">None</span></code> and <code class="docutils literal"><span class="pre">NULL</span></code></a></li>
<li><a class="reference internal" href="#support-for-void">Support for <code class="docutils literal"><span class="pre">void</span> <span class="pre">*</span></code></a></li>
<li><a class="reference internal" href="#super-and-pyqt4-classes"><code class="docutils literal"><span class="pre">super</span></code> and PyQt4 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" 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="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 class="nav-item nav-item-0"><a href="index.html">PyQt 4.12.1 Reference Guide</a> &#187;</li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &#169; Copyright 2016 Riverbank Computing Limited.
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.6.2.
    </div>
  </body>
</html>