<!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>New-style Signal and Slot Support — PyQt 4.10.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.10.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.10.3 Reference Guide" href="index.html" /> <link rel="next" title="Old-style Signal and Slot Support" href="old_style_signals_slots.html" /> <link rel="prev" title="Support for Qt Properties" href="qt_properties.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="old_style_signals_slots.html" title="Old-style Signal and Slot Support" accesskey="N">next</a> |</li> <li class="right" > <a href="qt_properties.html" title="Support for Qt Properties" accesskey="P">previous</a> |</li> <li><a href="index.html">PyQt 4.10.3 Reference Guide</a> »</li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body"> <div class="section" id="new-style-signal-and-slot-support"> <h1>New-style Signal and Slot Support<a class="headerlink" href="#new-style-signal-and-slot-support" title="Permalink to this headline">¶</a></h1> <p>This section describes the new style of connecting signals and slots introduced in PyQt4 v4.5.</p> <p>One of the key features of Qt is its use of signals and slots to communicate between objects. Their use encourages the development of reusable components.</p> <p>A signal is emitted when something of potential interest happens. A slot is a Python callable. If a signal is connected to a slot then the slot is called when the signal is emitted. If a signal isn’t connected then nothing happens. The code (or component) that emits the signal does not know or care if the signal is being used.</p> <p>The signal/slot mechanism has the following features.</p> <ul class="simple"> <li>A signal may be connected to many slots.</li> <li>A signal may also be connected to another signal.</li> <li>Signal arguments may be any Python type.</li> <li>A slot may be connected to many signals.</li> <li>Connections may be direct (ie. synchronous) or queued (ie. asynchronous).</li> <li>Connections may be made across threads.</li> <li>Signals may be disconnected.</li> </ul> <div class="section" id="unbound-and-bound-signals"> <h2>Unbound and Bound Signals<a class="headerlink" href="#unbound-and-bound-signals" title="Permalink to this headline">¶</a></h2> <p>A signal (specifically an unbound signal) is an attribute of a class that is a sub-class of <tt class="docutils literal"><span class="pre">QObject</span></tt>. When a signal is referenced as an attribute of an instance of the class then PyQt4 automatically binds the instance to the signal in order to create a <em>bound signal</em>. This is the same mechanism that Python itself uses to create bound methods from class functions.</p> <p>A bound signal has <tt class="docutils literal"><span class="pre">connect()</span></tt>, <tt class="docutils literal"><span class="pre">disconnect()</span></tt> and <tt class="docutils literal"><span class="pre">emit()</span></tt> methods that implement the associated functionality. It also has a <tt class="docutils literal"><span class="pre">signal</span></tt> attribute that is the signature of the signal that would be returned by Qt’s <tt class="docutils literal"><span class="pre">SIGNAL()</span></tt> macro.</p> <p>A signal may be overloaded, ie. a signal with a particular name may support more than one signature. A signal may be indexed with a signature in order to select the one required. A signature is a sequence of types. A type is either a Python type object or a string that is the name of a C++ type. The name of a C++ type is automatically normalised so that, for example, <tt class="docutils literal"><span class="pre">QString</span></tt> can be used instead of the non-normalised <tt class="docutils literal"><span class="pre">const</span> <span class="pre">QString</span> <span class="pre">&</span></tt>.</p> <p>If a signal is overloaded then it will have a default that will be used if no index is given.</p> <p>When a signal is emitted then any arguments are converted to C++ types if possible. If an argument doesn’t have a corresponding C++ type then it is wrapped in a special C++ type that allows it to be passed around Qt’s meta-type system while ensuring that its reference count is properly maintained.</p> </div> <div class="section" id="defining-new-signals-with-pyqtsignal"> <h2>Defining New Signals with <a class="reference internal" href="#PyQt4.QtCore.pyqtSignal" title="PyQt4.QtCore.pyqtSignal"><tt class="xref py py-func docutils literal"><span class="pre">pyqtSignal()</span></tt></a><a class="headerlink" href="#defining-new-signals-with-pyqtsignal" title="Permalink to this headline">¶</a></h2> <p>PyQt4 automatically defines signals for all Qt’s built-in signals. New signals can be defined as class attributes using the <a class="reference internal" href="#PyQt4.QtCore.pyqtSignal" title="PyQt4.QtCore.pyqtSignal"><tt class="xref py py-func docutils literal"><span class="pre">pyqtSignal()</span></tt></a> factory.</p> <dl class="function"> <dt id="PyQt4.QtCore.pyqtSignal"> <tt class="descclassname">PyQt4.QtCore.</tt><tt class="descname">pyqtSignal</tt><big>(</big><em>types</em><span class="optional">[</span>, <em>name</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#PyQt4.QtCore.pyqtSignal" title="Permalink to this definition">¶</a></dt> <dd><p>Create one or more overloaded unbound signals as a class attribute.</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"><ul class="first simple"> <li><strong>types</strong> – the types that define the C++ signature of the signal. Each type may be a Python type object or a string that is the name of a C++ type. Alternatively each may be a sequence of type arguments. In this case each sequence defines the signature of a different signal overload. The first overload will be the default.</li> <li><strong>name</strong> – the name of the signal. If it is omitted then the name of the class attribute is used. This may only be given as a keyword argument.</li> </ul> </td> </tr> <tr class="field-even field"><th class="field-name">Return type:</th><td class="field-body"><p class="first last">an unbound signal</p> </td> </tr> </tbody> </table> </dd></dl> <p>The following example shows the definition of a number of new signals:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">PyQt4.QtCore</span> <span class="kn">import</span> <span class="n">QObject</span><span class="p">,</span> <span class="n">pyqtSignal</span> <span class="k">class</span> <span class="nc">Foo</span><span class="p">(</span><span class="n">QObject</span><span class="p">):</span> <span class="c"># This defines a signal called 'closed' that takes no arguments.</span> <span class="n">closed</span> <span class="o">=</span> <span class="n">pyqtSignal</span><span class="p">()</span> <span class="c"># This defines a signal called 'rangeChanged' that takes two</span> <span class="c"># integer arguments.</span> <span class="n">range_changed</span> <span class="o">=</span> <span class="n">pyqtSignal</span><span class="p">(</span><span class="nb">int</span><span class="p">,</span> <span class="nb">int</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s">'rangeChanged'</span><span class="p">)</span> <span class="c"># This defines a signal called 'valueChanged' that has two overloads,</span> <span class="c"># one that takes an integer argument and one that takes a QString</span> <span class="c"># argument. Note that because we use a string to specify the type of</span> <span class="c"># the QString argument then this code will run under Python v2 and v3.</span> <span class="n">valueChanged</span> <span class="o">=</span> <span class="n">pyqtSignal</span><span class="p">([</span><span class="nb">int</span><span class="p">],</span> <span class="p">[</span><span class="s">'QString'</span><span class="p">])</span> </pre></div> </div> <p>New signals should only be defined in sub-classes of <tt class="docutils literal"><span class="pre">QObject</span></tt>.</p> <p>New signals defined in this way will be automatically added to the class’s <tt class="docutils literal"><span class="pre">QMetaObject</span></tt>. This means that they will appear in Qt Designer and can be introspected using the <tt class="docutils literal"><span class="pre">QMetaObject</span></tt> API.</p> <p>Overloaded signals should be used with care when an argument has a Python type that has no corresponding C++ type. PyQt4 uses the same internal C++ class to represent such objects and so it is possible to have overloaded signals with different Python signatures that are implemented with identical C++ signatures with unexpected results. The following is an example of this:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Foo</span><span class="p">(</span><span class="n">QObject</span><span class="p">):</span> <span class="c"># This will cause problems because each has the same C++ signature.</span> <span class="n">valueChanged</span> <span class="o">=</span> <span class="n">pyqtSignal</span><span class="p">([</span><span class="nb">dict</span><span class="p">],</span> <span class="p">[</span><span class="nb">list</span><span class="p">])</span> </pre></div> </div> </div> <div class="section" id="connecting-disconnecting-and-emitting-signals"> <h2>Connecting, Disconnecting and Emitting Signals<a class="headerlink" href="#connecting-disconnecting-and-emitting-signals" title="Permalink to this headline">¶</a></h2> <p>Signals are connected to slots using the <a class="reference internal" href="#connect" title="connect"><tt class="xref py py-meth docutils literal"><span class="pre">connect()</span></tt></a> method of a bound signal.</p> <dl class="method"> <dt id="connect"> <tt class="descname">connect</tt><big>(</big><em>slot</em><span class="optional">[</span>, <em>type=PyQt4.QtCore.Qt.AutoConnection</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#connect" title="Permalink to this definition">¶</a></dt> <dd><p>Connect a signal to a slot. An exception will be raised if the connection failed.</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"><ul class="first last simple"> <li><strong>slot</strong> – the slot to connect to, either a Python callable or another bound signal.</li> <li><strong>type</strong> – the type of the connection to make.</li> </ul> </td> </tr> </tbody> </table> </dd></dl> <p>Signals are disconnected from slots using the <a class="reference internal" href="#disconnect" title="disconnect"><tt class="xref py py-meth docutils literal"><span class="pre">disconnect()</span></tt></a> method of a bound signal.</p> <dl class="method"> <dt id="disconnect"> <tt class="descname">disconnect</tt><big>(</big><span class="optional">[</span><em>slot</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#disconnect" title="Permalink to this definition">¶</a></dt> <dd><p>Disconnect one or more slots from a signal. An exception will be raised if the slot is not connected to the signal or if the signal has no connections at all.</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>slot</strong> – the optional slot to disconnect from, either a Python callable or another bound signal. If it is omitted then all slots connected to the signal are disconnected.</td> </tr> </tbody> </table> </dd></dl> <p>Signals are emitted from using the <a class="reference internal" href="#emit" title="emit"><tt class="xref py py-meth docutils literal"><span class="pre">emit()</span></tt></a> method of a bound signal.</p> <dl class="method"> <dt id="emit"> <tt class="descname">emit</tt><big>(</big><em>*args</em><big>)</big><a class="headerlink" href="#emit" title="Permalink to this definition">¶</a></dt> <dd><p>Emit a signal.</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>args</strong> – the optional sequence of arguments to pass to any connected slots.</td> </tr> </tbody> </table> </dd></dl> <p>The following code demonstrates the definition, connection and emit of a signal without arguments:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">PyQt4.QtCore</span> <span class="kn">import</span> <span class="n">QObject</span><span class="p">,</span> <span class="n">pyqtSignal</span> <span class="k">class</span> <span class="nc">Foo</span><span class="p">(</span><span class="n">QObject</span><span class="p">):</span> <span class="c"># Define a new signal called 'trigger' that has no arguments.</span> <span class="n">trigger</span> <span class="o">=</span> <span class="n">pyqtSignal</span><span class="p">()</span> <span class="k">def</span> <span class="nf">connect_and_emit_trigger</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="c"># Connect the trigger signal to a slot.</span> <span class="bp">self</span><span class="o">.</span><span class="n">trigger</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">handle_trigger</span><span class="p">)</span> <span class="c"># Emit the signal.</span> <span class="bp">self</span><span class="o">.</span><span class="n">trigger</span><span class="o">.</span><span class="n">emit</span><span class="p">()</span> <span class="k">def</span> <span class="nf">handle_trigger</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="c"># Show that the slot has been called.</span> <span class="k">print</span> <span class="s">"trigger signal received"</span> </pre></div> </div> <p>The following code demonstrates the connection of overloaded signals:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">PyQt4.QtGui</span> <span class="kn">import</span> <span class="n">QComboBox</span> <span class="k">class</span> <span class="nc">Bar</span><span class="p">(</span><span class="n">QComboBox</span><span class="p">):</span> <span class="k">def</span> <span class="nf">connect_activated</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="c"># The PyQt4 documentation will define what the default overload is.</span> <span class="c"># In this case it is the overload with the single integer argument.</span> <span class="bp">self</span><span class="o">.</span><span class="n">activated</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">handle_int</span><span class="p">)</span> <span class="c"># For non-default overloads we have to specify which we want to</span> <span class="c"># connect. In this case the one with the single string argument.</span> <span class="c"># (Note that we could also explicitly specify the default if we</span> <span class="c"># wanted to.)</span> <span class="bp">self</span><span class="o">.</span><span class="n">activated</span><span class="p">[</span><span class="nb">str</span><span class="p">]</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">handle_string</span><span class="p">)</span> <span class="k">def</span> <span class="nf">handle_int</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">index</span><span class="p">):</span> <span class="k">print</span> <span class="s">"activated signal passed integer"</span><span class="p">,</span> <span class="n">index</span> <span class="k">def</span> <span class="nf">handle_string</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">text</span><span class="p">):</span> <span class="k">print</span> <span class="s">"activated signal passed QString"</span><span class="p">,</span> <span class="n">text</span> </pre></div> </div> </div> <div class="section" id="connecting-signals-using-keyword-arguments"> <h2>Connecting Signals Using Keyword Arguments<a class="headerlink" href="#connecting-signals-using-keyword-arguments" title="Permalink to this headline">¶</a></h2> <p>It is also possible to connect signals by passing a slot as a keyword argument corresponding to the name of the signal when creating an object, or using the <tt class="docutils literal"><span class="pre">pyqtConfigure()</span></tt> method of <tt class="docutils literal"><span class="pre">QObject</span></tt>. For example the following three fragments are equivalent:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">act</span> <span class="o">=</span> <span class="n">QtGui</span><span class="o">.</span><span class="n">QAction</span><span class="p">(</span><span class="s">"Action"</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span> <span class="n">act</span><span class="o">.</span><span class="n">triggered</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">on_triggered</span><span class="p">)</span> <span class="n">act</span> <span class="o">=</span> <span class="n">QtGui</span><span class="o">.</span><span class="n">QAction</span><span class="p">(</span><span class="s">"Action"</span><span class="p">,</span> <span class="bp">self</span><span class="p">,</span> <span class="n">triggered</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">on_triggered</span><span class="p">)</span> <span class="n">act</span> <span class="o">=</span> <span class="n">QtGui</span><span class="o">.</span><span class="n">QAction</span><span class="p">(</span><span class="s">"Action"</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span> <span class="n">act</span><span class="o">.</span><span class="n">pyqtConfigure</span><span class="p">(</span><span class="n">triggered</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">on_triggered</span><span class="p">)</span> </pre></div> </div> </div> <div class="section" id="the-pyqtslot-decorator"> <h2>The <a class="reference internal" href="#PyQt4.QtCore.pyqtSlot" title="PyQt4.QtCore.pyqtSlot"><tt class="xref py py-func docutils literal"><span class="pre">pyqtSlot()</span></tt></a> Decorator<a class="headerlink" href="#the-pyqtslot-decorator" title="Permalink to this headline">¶</a></h2> <p>Although PyQt4 allows any Python callable to be used as a slot when connecting signals, it is sometimes necessary to explicitly mark a Python method as being a Qt slot and to provide a C++ signature for it. PyQt4 provides the <a class="reference internal" href="#PyQt4.QtCore.pyqtSlot" title="PyQt4.QtCore.pyqtSlot"><tt class="xref py py-func docutils literal"><span class="pre">pyqtSlot()</span></tt></a> function decorator to do this.</p> <dl class="function"> <dt id="PyQt4.QtCore.pyqtSlot"> <tt class="descclassname">PyQt4.QtCore.</tt><tt class="descname">pyqtSlot</tt><big>(</big><em>types[, name][, result]</em><big>)</big><a class="headerlink" href="#PyQt4.QtCore.pyqtSlot" title="Permalink to this definition">¶</a></dt> <dd><p>Decorate a Python method to create a Qt slot.</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"><ul class="first last simple"> <li><strong>types</strong> – the types that define the C++ signature of the slot. Each type may be a Python type object or a string that is the name of a C++ type.</li> <li><strong>name</strong> – the name of the slot that will be seen by C++. If omitted the name of the Python method being decorated will be used. This may only be given as a keyword argument.</li> <li><strong>result</strong> – the type of the result and may be a Python type object or a string that specifies a C++ type. This may only be given as a keyword argument.</li> </ul> </td> </tr> </tbody> </table> </dd></dl> <p>Connecting a signal to a decorated Python method also has the advantage of reducing the amount of memory used and is slightly faster.</p> <p>For example:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">PyQt4.QtCore</span> <span class="kn">import</span> <span class="n">QObject</span><span class="p">,</span> <span class="n">pyqtSlot</span> <span class="k">class</span> <span class="nc">Foo</span><span class="p">(</span><span class="n">QObject</span><span class="p">):</span> <span class="nd">@pyqtSlot</span><span class="p">()</span> <span class="k">def</span> <span class="nf">foo</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="sd">""" C++: void foo() """</span> <span class="nd">@pyqtSlot</span><span class="p">(</span><span class="nb">int</span><span class="p">,</span> <span class="nb">str</span><span class="p">)</span> <span class="k">def</span> <span class="nf">foo</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">arg1</span><span class="p">,</span> <span class="n">arg2</span><span class="p">):</span> <span class="sd">""" C++: void foo(int, QString) """</span> <span class="nd">@pyqtSlot</span><span class="p">(</span><span class="nb">int</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s">'bar'</span><span class="p">)</span> <span class="k">def</span> <span class="nf">foo</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">arg1</span><span class="p">):</span> <span class="sd">""" C++: void bar(int) """</span> <span class="nd">@pyqtSlot</span><span class="p">(</span><span class="nb">int</span><span class="p">,</span> <span class="n">result</span><span class="o">=</span><span class="nb">int</span><span class="p">)</span> <span class="k">def</span> <span class="nf">foo</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">arg1</span><span class="p">):</span> <span class="sd">""" C++: int foo(int) """</span> <span class="nd">@pyqtSlot</span><span class="p">(</span><span class="nb">int</span><span class="p">,</span> <span class="n">QObject</span><span class="p">)</span> <span class="k">def</span> <span class="nf">foo</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">arg1</span><span class="p">):</span> <span class="sd">""" C++: int foo(int, QObject *) """</span> </pre></div> </div> <p>It is also possible to chain the decorators in order to define a Python method several times with different signatures. For example:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">PyQt4.QtCore</span> <span class="kn">import</span> <span class="n">QObject</span><span class="p">,</span> <span class="n">pyqtSlot</span> <span class="k">class</span> <span class="nc">Foo</span><span class="p">(</span><span class="n">QObject</span><span class="p">):</span> <span class="nd">@pyqtSlot</span><span class="p">(</span><span class="nb">int</span><span class="p">)</span> <span class="nd">@pyqtSlot</span><span class="p">(</span><span class="s">'QString'</span><span class="p">)</span> <span class="k">def</span> <span class="nf">valueChanged</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span> <span class="sd">""" Two slots will be defined in the QMetaObject. """</span> </pre></div> </div> </div> <div class="section" id="connecting-slots-by-name"> <h2>Connecting Slots By Name<a class="headerlink" href="#connecting-slots-by-name" title="Permalink to this headline">¶</a></h2> <p>PyQt4 supports the <tt class="docutils literal"><span class="pre">QtCore.QMetaObject.connectSlotsByName()</span></tt> function that is most commonly used by <strong class="program">pyuic4</strong> generated Python code to automatically connect signals to slots that conform to a simple naming convention. However, where a class has overloaded Qt signals (ie. with the same name but with different arguments) PyQt4 needs additional information in order to automatically connect the correct signal.</p> <p>For example the <tt class="docutils literal"><span class="pre">QtGui.QSpinBox</span></tt> class has the following signals:</p> <div class="highlight-python"><pre>void valueChanged(int i); void valueChanged(const QString &text);</pre> </div> <p>When the value of the spin box changes both of these signals will be emitted. If you have implemented a slot called <tt class="docutils literal"><span class="pre">on_spinbox_valueChanged</span></tt> (which assumes that you have given the <tt class="docutils literal"><span class="pre">QSpinBox</span></tt> instance the name <tt class="docutils literal"><span class="pre">spinbox</span></tt>) then it will be connected to both variations of the signal. Therefore, when the user changes the value, your slot will be called twice - once with an integer argument, and once with a unicode or <tt class="docutils literal"><span class="pre">QString</span></tt> argument.</p> <p>This also happens with signals that take optional arguments. Qt implements this using multiple signals. For example, <tt class="docutils literal"><span class="pre">QtGui.QAbstractButton</span></tt> has the following signal:</p> <div class="highlight-python"><pre>void clicked(bool checked = false);</pre> </div> <p>Qt implements this as the following:</p> <div class="highlight-python"><pre>void clicked(); void clicked(bool checked);</pre> </div> <p>The <a class="reference internal" href="#PyQt4.QtCore.pyqtSlot" title="PyQt4.QtCore.pyqtSlot"><tt class="xref py py-func docutils literal"><span class="pre">pyqtSlot()</span></tt></a> decorator can be used to specify which of the signals should be connected to the slot.</p> <p>For example, if you were only interested in the integer variant of the signal then your slot definition would look like the following:</p> <div class="highlight-python"><div class="highlight"><pre><span class="nd">@pyqtSlot</span><span class="p">(</span><span class="nb">int</span><span class="p">)</span> <span class="k">def</span> <span class="nf">on_spinbox_valueChanged</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">i</span><span class="p">):</span> <span class="c"># i will be an integer.</span> <span class="k">pass</span> </pre></div> </div> <p>If you wanted to handle both variants of the signal, but with different Python methods, then your slot definitions might look like the following:</p> <div class="highlight-python"><div class="highlight"><pre><span class="nd">@pyqtSlot</span><span class="p">(</span><span class="nb">int</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s">'on_spinbox_valueChanged'</span><span class="p">)</span> <span class="k">def</span> <span class="nf">spinbox_int_value</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">i</span><span class="p">):</span> <span class="c"># i will be an integer.</span> <span class="k">pass</span> <span class="nd">@pyqtSlot</span><span class="p">(</span><span class="nb">str</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s">'on_spinbox_valueChanged'</span><span class="p">)</span> <span class="k">def</span> <span class="nf">spinbox_qstring_value</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">s</span><span class="p">):</span> <span class="c"># s will be a Python string object (or a QString if they are enabled).</span> <span class="k">pass</span> </pre></div> </div> <p>The following shows an example using a button when you are not interested in the optional argument:</p> <div class="highlight-python"><div class="highlight"><pre><span class="nd">@pyqtSlot</span><span class="p">()</span> <span class="k">def</span> <span class="nf">on_button_clicked</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="k">pass</span> </pre></div> </div> </div> <div class="section" id="mixing-new-style-and-old-style-connections"> <h2>Mixing New-style and Old-style Connections<a class="headerlink" href="#mixing-new-style-and-old-style-connections" title="Permalink to this headline">¶</a></h2> <p>The implementation of new-style connections is slightly different to the implementation of old-style connections. An application can freely use both styles subject to the restriction that any individual new-style connection should only be disconnected using the new style. Similarly any individual old-style connection should only be disconnected using the old style.</p> <p>You should also be aware that <strong class="program">pyuic4</strong> generates code that uses old-style connections.</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="#">New-style Signal and Slot Support</a><ul> <li><a class="reference internal" href="#unbound-and-bound-signals">Unbound and Bound Signals</a></li> <li><a class="reference internal" href="#defining-new-signals-with-pyqtsignal">Defining New Signals with <tt class="docutils literal"><span class="pre">pyqtSignal()</span></tt></a></li> <li><a class="reference internal" href="#connecting-disconnecting-and-emitting-signals">Connecting, Disconnecting and Emitting Signals</a></li> <li><a class="reference internal" href="#connecting-signals-using-keyword-arguments">Connecting Signals Using Keyword Arguments</a></li> <li><a class="reference internal" href="#the-pyqtslot-decorator">The <tt class="docutils literal"><span class="pre">pyqtSlot()</span></tt> Decorator</a></li> <li><a class="reference internal" href="#connecting-slots-by-name">Connecting Slots By Name</a></li> <li><a class="reference internal" href="#mixing-new-style-and-old-style-connections">Mixing New-style and Old-style Connections</a></li> </ul> </li> </ul> <h4>Previous topic</h4> <p class="topless"><a href="qt_properties.html" title="previous chapter">Support for Qt Properties</a></p> <h4>Next topic</h4> <p class="topless"><a href="old_style_signals_slots.html" title="next chapter">Old-style Signal and Slot Support</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="old_style_signals_slots.html" title="Old-style Signal and Slot Support" >next</a> |</li> <li class="right" > <a href="qt_properties.html" title="Support for Qt Properties" >previous</a> |</li> <li><a href="index.html">PyQt 4.10.3 Reference Guide</a> »</li> </ul> </div> <div class="footer"> © Copyright 2013 Riverbank Computing Limited. Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3. </div> </body> </html>