Sophie

Sophie

distrib > Mageia > 4 > x86_64 > by-pkgid > e3639387f84ff7e4577bf5ae1db4d38f > files > 1632

python-qt5-doc-5.1.1-1.mga4.noarch.rpm



<!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>Support for Signals and Slots &mdash; PyQt 5.1.1 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:     '5.1.1',
        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 5.1.1 Reference Guide" href="index.html" />
    <link rel="next" title="Support for Qt Properties" href="qt_properties.html" />
    <link rel="prev" title="QXmlStreamWriter" href="api/qxmlstreamwriter.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="qt_properties.html" title="Support for Qt Properties"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="api/qxmlstreamwriter.html" title="QXmlStreamWriter"
             accesskey="P">previous</a> |</li>
        <li><a href="index.html">PyQt 5.1.1 Reference Guide</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="support-for-signals-and-slots">
<h1>Support for Signals and Slots<a class="headerlink" href="#support-for-signals-and-slots" title="Permalink to this headline">¶</a></h1>
<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&#8217;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 <a class="reference internal" href="api/qobject.html#PyQt5.QtCore.QObject" title="PyQt5.QtCore.QObject"><tt class="xref py py-class docutils literal"><span class="pre">QObject</span></tt></a>.  When a signal is referenced as an
attribute of an instance of the class then PyQt5 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&#8217;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">QVariant</span></tt> can be
used instead of the non-normalised <tt class="docutils literal"><span class="pre">const</span> <span class="pre">QVariant</span> <span class="pre">&amp;</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&#8217;t have a corresponding C++ type then it is
wrapped in a special C++ type that allows it to be passed around Qt&#8217;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="#PyQt5.QtCore.pyqtSignal" title="PyQt5.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>PyQt5 automatically defines signals for all Qt&#8217;s built-in signals.  New signals
can be defined as class attributes using the <a class="reference internal" href="#PyQt5.QtCore.pyqtSignal" title="PyQt5.QtCore.pyqtSignal"><tt class="xref py py-func docutils literal"><span class="pre">pyqtSignal()</span></tt></a>
factory.</p>
<dl class="function">
<dt id="PyQt5.QtCore.pyqtSignal">
<tt class="descclassname">PyQt5.QtCore.</tt><tt class="descname">pyqtSignal</tt><big>(</big><em>types[, name][, revision=0][, arguments=[]]</em><big>)</big><a class="headerlink" href="#PyQt5.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> &#8211; 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> &#8211; 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>
<li><strong>revision</strong> &#8211; the revision of the signal that is exported to QML.  This may only be
given as a keyword argument.</li>
<li><strong>arguments</strong> &#8211; the sequence of the names of the signal&#8217;s arguments that is exported to
QML.  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">PyQt5.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 &#39;closed&#39; 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 &#39;rangeChanged&#39; 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">&#39;rangeChanged&#39;</span><span class="p">)</span>

    <span class="c"># This defines a signal called &#39;valueChanged&#39; 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">&#39;QString&#39;</span><span class="p">])</span>
</pre></div>
</div>
<p>New signals should only be defined in sub-classes of
<a class="reference internal" href="api/qobject.html#PyQt5.QtCore.QObject" title="PyQt5.QtCore.QObject"><tt class="xref py py-class docutils literal"><span class="pre">QObject</span></tt></a>.</p>
<p>New signals defined in this way will be automatically added to the class&#8217;s
<a class="reference internal" href="api/qmetaobject.html#PyQt5.QtCore.QMetaObject" title="PyQt5.QtCore.QMetaObject"><tt class="xref py py-class docutils literal"><span class="pre">QMetaObject</span></tt></a>.  This means that they will appear in Qt
Designer and can be introspected using the <a class="reference internal" href="api/qmetaobject.html#PyQt5.QtCore.QMetaObject" title="PyQt5.QtCore.QMetaObject"><tt class="xref py py-class docutils literal"><span class="pre">QMetaObject</span></tt></a>
API.</p>
<p>Overloaded signals should be used with care when an argument has a Python type
that has no corresponding C++ type.  PyQt5 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=PyQt5.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> &#8211; the slot to connect to, either a Python callable or another bound
signal.</li>
<li><strong>type</strong> &#8211; 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> &#8211; 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> &#8211; 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">PyQt5.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 &#39;trigger&#39; 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">&quot;trigger signal received&quot;</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">PyQt5.QtWidgets</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 PyQt5 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">&quot;activated signal passed integer&quot;</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">&quot;activated signal passed QString&quot;</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="xref py py-meth docutils literal"><span class="pre">pyqtConfigure()</span></tt> method.  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">QAction</span><span class="p">(</span><span class="s">&quot;Action&quot;</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">QAction</span><span class="p">(</span><span class="s">&quot;Action&quot;</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">QAction</span><span class="p">(</span><span class="s">&quot;Action&quot;</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="#PyQt5.QtCore.pyqtSlot" title="PyQt5.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 PyQt5 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.  PyQt5 provides the
<a class="reference internal" href="#PyQt5.QtCore.pyqtSlot" title="PyQt5.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="PyQt5.QtCore.pyqtSlot">
<tt class="descclassname">PyQt5.QtCore.</tt><tt class="descname">pyqtSlot</tt><big>(</big><em>types[, name][, result][, revision=0]</em><big>)</big><a class="headerlink" href="#PyQt5.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> &#8211; 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> &#8211; 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>revision</strong> &#8211; the revision of the slot that is exported to QML.  This may only be
given as a keyword argument.</li>
<li><strong>result</strong> &#8211; 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">PyQt5.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">&quot;&quot;&quot; C++: void foo() &quot;&quot;&quot;</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">&quot;&quot;&quot; C++: void foo(int, QString) &quot;&quot;&quot;</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">&#39;bar&#39;</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">&quot;&quot;&quot; C++: void bar(int) &quot;&quot;&quot;</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">&quot;&quot;&quot; C++: int foo(int) &quot;&quot;&quot;</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">&quot;&quot;&quot; C++: int foo(int, QObject *) &quot;&quot;&quot;</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">PyQt5.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">&#39;QString&#39;</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">&quot;&quot;&quot; Two slots will be defined in the QMetaObject. &quot;&quot;&quot;</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>PyQt5 supports the <tt class="xref py py-meth docutils literal"><span class="pre">connectSlotsByName()</span></tt> function
that is most commonly used by <strong class="program">pyuic5</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) PyQt5 needs additional information in
order to automatically connect the correct signal.</p>
<p>For example the <a class="reference internal" href="api/qspinbox.html#PyQt5.QtWidgets.QSpinBox" title="PyQt5.QtWidgets.QSpinBox"><tt class="xref py py-class docutils literal"><span class="pre">QSpinBox</span></tt></a> class has the following
signals:</p>
<div class="highlight-python"><pre>void valueChanged(int i);
void valueChanged(const QString &amp;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="xref py py-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 string argument.</p>
<p>This also happens with signals that take optional arguments.  Qt implements
this using multiple signals.  For example,
<a class="reference internal" href="api/qabstractbutton.html#PyQt5.QtWidgets.QAbstractButton" title="PyQt5.QtWidgets.QAbstractButton"><tt class="xref py py-class docutils literal"><span class="pre">QAbstractButton</span></tt></a> 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="#PyQt5.QtCore.pyqtSlot" title="PyQt5.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">&#39;on_spinbox_valueChanged&#39;</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">&#39;on_spinbox_valueChanged&#39;</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>


          </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="#">Support for Signals and Slots</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>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="api/qxmlstreamwriter.html"
                        title="previous chapter">QXmlStreamWriter</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="qt_properties.html"
                        title="next chapter">Support for Qt Properties</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="qt_properties.html" title="Support for Qt Properties"
             >next</a> |</li>
        <li class="right" >
          <a href="api/qxmlstreamwriter.html" title="QXmlStreamWriter"
             >previous</a> |</li>
        <li><a href="index.html">PyQt 5.1.1 Reference Guide</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2013 Riverbank Computing Limited.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3.
    </div>
  </body>
</html>