Sophie

Sophie

distrib > Mageia > 4 > i586 > by-pkgid > 28b9e36e96ce34b2567ae5b47a27b2c5 > files > 1133

python-qt4-doc-4.10.3-3.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 Qt Properties &mdash; 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="New-style Signal and Slot Support" href="new_style_signals_slots.html" />
    <link rel="prev" title="Support for Keyword Arguments" href="keyword_arguments.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="new_style_signals_slots.html" title="New-style Signal and Slot Support"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="keyword_arguments.html" title="Support for Keyword Arguments"
             accesskey="P">previous</a> |</li>
        <li><a href="index.html">PyQt 4.10.3 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-qt-properties">
<h1>Support for Qt Properties<a class="headerlink" href="#support-for-qt-properties" title="Permalink to this headline">¶</a></h1>
<p>PyQt4 does not support the setting and getting of Qt properties as if they were
normal instance attributes.  This is because the name of a property often
conflicts with the name of the property&#8217;s getter method.</p>
<p>However, PyQt4 does support the initial setting of properties using keyword
arguments passed when an instance is created.  For example:</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">&quot;&amp;Save&quot;</span><span class="p">,</span> <span class="bp">self</span><span class="p">,</span> <span class="n">shortcut</span><span class="o">=</span><span class="n">QtGui</span><span class="o">.</span><span class="n">QKeySequence</span><span class="o">.</span><span class="n">Save</span><span class="p">,</span>
        <span class="n">statusTip</span><span class="o">=</span><span class="s">&quot;Save the document to disk&quot;</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">save</span><span class="p">)</span>
</pre></div>
</div>
<p>The example also demonstrates the use of a keyword argument to connect a
signal to a slot.</p>
<p>PyQt4 also supports setting the values of properties (and connecting a signal
to a slot) 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 gives the same results as above:</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">&quot;&amp;Save&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">shortcut</span><span class="o">=</span><span class="n">QtGui</span><span class="o">.</span><span class="n">QKeySequence</span><span class="o">.</span><span class="n">Save</span><span class="p">,</span>
        <span class="n">statusTip</span><span class="o">=</span><span class="s">&quot;Save the document to disk&quot;</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">save</span><span class="p">)</span>
</pre></div>
</div>
<div class="section" id="defining-new-qt-properties">
<h2>Defining New Qt Properties<a class="headerlink" href="#defining-new-qt-properties" title="Permalink to this headline">¶</a></h2>
<p>A new Qt property may be defined using the <a class="reference internal" href="#PyQt4.QtCore.pyqtProperty" title="PyQt4.QtCore.pyqtProperty"><tt class="xref py py-func docutils literal"><span class="pre">pyqtProperty()</span></tt></a>
function.  It is used in the same way as the standard Python <tt class="docutils literal"><span class="pre">property()</span></tt>
function.  In fact, Qt properties defined in this way also behave as Python
properties.</p>
<dl class="function">
<dt id="PyQt4.QtCore.pyqtProperty">
<tt class="descclassname">PyQt4.QtCore.</tt><tt class="descname">pyqtProperty</tt><big>(</big><em>type</em><span class="optional">[</span>, <em>fget=None</em><span class="optional">[</span>, <em>fset=None</em><span class="optional">[</span>, <em>freset=None</em><span class="optional">[</span>, <em>fdel=None</em><span class="optional">[</span>, <em>doc=None</em><span class="optional">[</span>, <em>designable=True</em><span class="optional">[</span>, <em>scriptable=True</em><span class="optional">[</span>, <em>stored=True</em><span class="optional">[</span>, <em>user=False</em><span class="optional">[</span>, <em>constant=False</em><span class="optional">[</span>, <em>final=False</em><span class="optional">[</span>, <em>notify=None</em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#PyQt4.QtCore.pyqtProperty" title="Permalink to this definition">¶</a></dt>
<dd><p>Create a property that behaves as both a Python property and a Qt property.</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>type</strong> &#8211; the type of the property.  It is either a Python type object or a
string that is the name of a C++ type.</li>
<li><strong>fget</strong> &#8211; the optional callable used to get the value of the property.</li>
<li><strong>fset</strong> &#8211; the optional callable used to set the value of the property.</li>
<li><strong>freset</strong> &#8211; the optional callable used to reset the value of the property to its
default value.  It is ignored by Python</li>
<li><strong>fdel</strong> &#8211; the optional callable used to delete the property.  It is ignored by
Qt.</li>
<li><strong>doc</strong> &#8211; the optional docstring of the property.  It is ignored by Qt.</li>
<li><strong>designable</strong> &#8211; optionally sets the Qt <tt class="docutils literal"><span class="pre">DESIGNABLE</span></tt> flag.  It is ignored by Python</li>
<li><strong>scriptable</strong> &#8211; optionally sets the Qt <tt class="docutils literal"><span class="pre">SCRIPTABLE</span></tt> flag.  It is ignored by Python</li>
<li><strong>stored</strong> &#8211; optionally sets the Qt <tt class="docutils literal"><span class="pre">STORED</span></tt> flag.  It is ignored by Python</li>
<li><strong>user</strong> &#8211; optionally sets the Qt <tt class="docutils literal"><span class="pre">USER</span></tt> flag.  It is ignored by Python</li>
<li><strong>constant</strong> &#8211; optionally sets the Qt <tt class="docutils literal"><span class="pre">CONSTANT</span></tt> flag.  It is ignored by Python</li>
<li><strong>final</strong> &#8211; optionally sets the Qt <tt class="docutils literal"><span class="pre">FINAL</span></tt> flag.  It is ignored by Python</li>
<li><strong>notify</strong> &#8211; the optional unbound notify signal.  It is ignored by Python</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Return type:</th><td class="field-body"><p class="first last">the property object.</p>
</td>
</tr>
</tbody>
</table>
</dd></dl>

<p>It is also possible to use <a class="reference internal" href="#PyQt4.QtCore.pyqtProperty" title="PyQt4.QtCore.pyqtProperty"><tt class="xref py py-func docutils literal"><span class="pre">pyqtProperty()</span></tt></a> as a decorator in
the same way as the standard Python <tt class="docutils literal"><span class="pre">property()</span></tt> function.  The following
example shows how to define an <tt class="docutils literal"><span class="pre">int</span></tt> property with a getter and setter:</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">pyqtProperty</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="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">QObject</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_total</span> <span class="o">=</span> <span class="mi">0</span>

    <span class="nd">@pyqtProperty</span><span class="p">(</span><span class="nb">int</span><span class="p">)</span>
    <span class="k">def</span> <span class="nf">total</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_total</span>

    <span class="nd">@total.setter</span>
    <span class="k">def</span> <span class="nf">total</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="bp">self</span><span class="o">.</span><span class="n">_total</span> <span class="o">=</span> <span class="n">value</span>
</pre></div>
</div>
<p>If you prefer the Qt terminology you may also use <tt class="docutils literal"><span class="pre">write</span></tt> instead of
<tt class="docutils literal"><span class="pre">setter</span></tt> (and <tt class="docutils literal"><span class="pre">read</span></tt> instead of <tt class="docutils literal"><span class="pre">getter</span></tt>).</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="#">Support for Qt Properties</a><ul>
<li><a class="reference internal" href="#defining-new-qt-properties">Defining New Qt Properties</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="keyword_arguments.html"
                        title="previous chapter">Support for Keyword Arguments</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="new_style_signals_slots.html"
                        title="next chapter">New-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="new_style_signals_slots.html" title="New-style Signal and Slot Support"
             >next</a> |</li>
        <li class="right" >
          <a href="keyword_arguments.html" title="Support for Keyword Arguments"
             >previous</a> |</li>
        <li><a href="index.html">PyQt 4.10.3 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>