Sophie

Sophie

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

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>Using Qt Designer &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="The PyQt4 Resource System" href="resources.html" />
    <link rel="prev" title="Things to be Aware Of" href="gotchas.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="resources.html" title="The PyQt4 Resource System"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="gotchas.html" title="Things to be Aware Of"
             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="using-qt-designer">
<h1>Using Qt Designer<a class="headerlink" href="#using-qt-designer" title="Permalink to this headline">¶</a></h1>
<p>Qt Designer is the Qt tool for designing and building graphical user
interfaces.  It allows you to design widgets, dialogs or complete main windows
using on-screen forms and a simple drag-and-drop interface.  It has the ability
to preview your designs to ensure they work as you intended, and to allow you
to prototype them with your users, before you have to write any code.</p>
<p>Qt Designer uses XML <tt class="docutils literal"><span class="pre">.ui</span></tt> files to store designs and does not generate any
code itself.  Qt includes the <tt class="docutils literal"><span class="pre">uic</span></tt> utility that generates the C++ code that
creates the user interface.  Qt also includes the <tt class="docutils literal"><span class="pre">QUiLoader</span></tt> class that
allows an application to load a <tt class="docutils literal"><span class="pre">.ui</span></tt> file and to create the corresponding
user interface dynamically.</p>
<p>PyQt4 does not wrap the <tt class="docutils literal"><span class="pre">QUiLoader</span></tt> class but instead includes the
<a class="reference internal" href="#module-PyQt4.uic" title="PyQt4.uic"><tt class="xref py py-mod docutils literal"><span class="pre">uic</span></tt></a> Python module.  Like <tt class="docutils literal"><span class="pre">QUiLoader</span></tt> this module can load
<tt class="docutils literal"><span class="pre">.ui</span></tt> files to create a user interface dynamically.  Like the <strong class="program">uic</strong>
utility it can also generate the Python code that will create the user
interface.  PyQt4&#8217;s <strong class="program">pyuic4</strong> utility is a command line interface to
the <a class="reference internal" href="#module-PyQt4.uic" title="PyQt4.uic"><tt class="xref py py-mod docutils literal"><span class="pre">uic</span></tt></a> module.  Both are described in detail in the following
sections.</p>
<div class="section" id="using-the-generated-code">
<h2>Using the Generated Code<a class="headerlink" href="#using-the-generated-code" title="Permalink to this headline">¶</a></h2>
<p>The code that is generated has an identical structure to that generated by Qt&#8217;s
<tt class="docutils literal"><span class="pre">uic</span></tt> and can be used in the same way.</p>
<p>The code is structured as a single class that is derived from the Python
<tt class="docutils literal"><span class="pre">object</span></tt> type.  The name of the class is the name of the toplevel object set
in Designer with <tt class="docutils literal"><span class="pre">Ui_</span></tt> prepended.  (In the C++ version the class is defined
in the <tt class="docutils literal"><span class="pre">Ui</span></tt> namespace.)  We refer to this class as the <em>form class</em>.</p>
<p>The class contains a method called <tt class="docutils literal"><span class="pre">setupUi()</span></tt>.  This takes a single argument
which is the widget in which the user interface is created.  The type of this
argument (typically <tt class="docutils literal"><span class="pre">QDialog</span></tt>, <tt class="docutils literal"><span class="pre">QWidget</span></tt> or <tt class="docutils literal"><span class="pre">QMainWindow</span></tt>) is set in
Designer.  We refer to this type as the <em>Qt base class</em>.</p>
<p>In the following examples we assume that a <tt class="docutils literal"><span class="pre">.ui</span></tt> file has been created
containing a dialog and the name of the <tt class="docutils literal"><span class="pre">QDialog</span></tt> object is <tt class="docutils literal"><span class="pre">ImageDialog</span></tt>.
We also assume that the name of the file containing the generated Python code
is <tt class="file docutils literal"><span class="pre">ui_imagedialog.py</span></tt>.  The generated code can then be used in a number
of ways.</p>
<p>The first example shows the direct approach where we simply create a simple
application to create the dialog:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">sys</span>
<span class="kn">from</span> <span class="nn">PyQt4.QtGui</span> <span class="kn">import</span> <span class="n">QApplication</span><span class="p">,</span> <span class="n">QDialog</span>
<span class="kn">from</span> <span class="nn">ui_imagedialog</span> <span class="kn">import</span> <span class="n">Ui_ImageDialog</span>

<span class="n">app</span> <span class="o">=</span> <span class="n">QApplication</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">)</span>
<span class="n">window</span> <span class="o">=</span> <span class="n">QDialog</span><span class="p">()</span>
<span class="n">ui</span> <span class="o">=</span> <span class="n">Ui_ImageDialog</span><span class="p">()</span>
<span class="n">ui</span><span class="o">.</span><span class="n">setupUi</span><span class="p">(</span><span class="n">window</span><span class="p">)</span>

<span class="n">window</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
<span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="n">app</span><span class="o">.</span><span class="n">exec_</span><span class="p">())</span>
</pre></div>
</div>
<p>The second example shows the single inheritance approach where we sub-class
<tt class="docutils literal"><span class="pre">QDialog</span></tt> and set up the user interface in the <tt class="docutils literal"><span class="pre">__init__()</span></tt> method:</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">QDialog</span>
<span class="kn">from</span> <span class="nn">ui_imagedialog</span> <span class="kn">import</span> <span class="n">Ui_ImageDialog</span>

<span class="k">class</span> <span class="nc">ImageDialog</span><span class="p">(</span><span class="n">QDialog</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">QDialog</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="c"># Set up the user interface from Designer.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ui</span> <span class="o">=</span> <span class="n">Ui_ImageDialog</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ui</span><span class="o">.</span><span class="n">setupUi</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>

        <span class="c"># Make some local modifications.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ui</span><span class="o">.</span><span class="n">colorDepthCombo</span><span class="o">.</span><span class="n">addItem</span><span class="p">(</span><span class="s">&quot;2 colors (1 bit per pixel)&quot;</span><span class="p">)</span>

        <span class="c"># Connect up the buttons.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ui</span><span class="o">.</span><span class="n">okButton</span><span class="o">.</span><span class="n">clicked</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">accept</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ui</span><span class="o">.</span><span class="n">cancelButton</span><span class="o">.</span><span class="n">clicked</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">reject</span><span class="p">)</span>
</pre></div>
</div>
<p>The third example shows the multiple inheritance approach:</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">QDialog</span>
<span class="kn">from</span> <span class="nn">ui_imagedialog</span> <span class="kn">import</span> <span class="n">Ui_ImageDialog</span>

<span class="k">class</span> <span class="nc">ImageDialog</span><span class="p">(</span><span class="n">QDialog</span><span class="p">,</span> <span class="n">Ui_ImageDialog</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">QDialog</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="c"># Set up the user interface from Designer.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">setupUi</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>

        <span class="c"># Make some local modifications.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">colorDepthCombo</span><span class="o">.</span><span class="n">addItem</span><span class="p">(</span><span class="s">&quot;2 colors (1 bit per pixel)&quot;</span><span class="p">)</span>

        <span class="c"># Connect up the buttons.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">okButton</span><span class="o">.</span><span class="n">clicked</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">accept</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">cancelButton</span><span class="o">.</span><span class="n">clicked</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">reject</span><span class="p">)</span>
</pre></div>
</div>
<p>It is also possible to use the same approach used in PyQt v3.  This is shown in
the final example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">ui_imagedialog</span> <span class="kn">import</span> <span class="n">ImageDialog</span>

<span class="k">class</span> <span class="nc">MyImageDialog</span><span class="p">(</span><span class="n">ImageDialog</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">ImageDialog</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="c"># Make some local modifications.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">colorDepthCombo</span><span class="o">.</span><span class="n">addItem</span><span class="p">(</span><span class="s">&quot;2 colors (1 bit per pixel)&quot;</span><span class="p">)</span>

        <span class="c"># Connect up the buttons.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">okButton</span><span class="o">.</span><span class="n">clicked</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">accept</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">cancelButton</span><span class="o">.</span><span class="n">clicked</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">reject</span><span class="p">)</span>
</pre></div>
</div>
<p>For a full description see the Qt Designer Manual in the Qt Documentation.</p>
</div>
<div class="section" id="the-uic-module">
<h2>The <a class="reference internal" href="#module-PyQt4.uic" title="PyQt4.uic"><tt class="xref py py-mod docutils literal"><span class="pre">uic</span></tt></a> Module<a class="headerlink" href="#the-uic-module" title="Permalink to this headline">¶</a></h2>
<p>The <a class="reference internal" href="#module-PyQt4.uic" title="PyQt4.uic"><tt class="xref py py-mod docutils literal"><span class="pre">uic</span></tt></a> module contains the following functions and objects.</p>
<span class="target" id="module-PyQt4.uic"></span><dl class="data">
<dt id="PyQt4.uic.widgetPluginPath">
<tt class="descclassname">PyQt4.uic.</tt><tt class="descname">widgetPluginPath</tt><a class="headerlink" href="#PyQt4.uic.widgetPluginPath" title="Permalink to this definition">¶</a></dt>
<dd><p>The list of the directories that are searched for widget plugins.
Initially it contains the name of the directory that contains the widget
plugins included with PyQt4.</p>
</dd></dl>

<dl class="function">
<dt id="PyQt4.uic.compileUi">
<tt class="descclassname">PyQt4.uic.</tt><tt class="descname">compileUi</tt><big>(</big><em>uifile</em>, <em>pyfile</em><span class="optional">[</span>, <em>execute=False</em><span class="optional">[</span>, <em>indent=4</em><span class="optional">[</span>, <em>pyqt3_wrapper=False</em><span class="optional">[</span>, <em>from_imports=False</em><span class="optional">[</span>, <em>resource_suffix='_rc'</em><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.uic.compileUi" title="Permalink to this definition">¶</a></dt>
<dd><p>Generate a Python module that will create a user interface from a Qt
Designer <tt class="docutils literal"><span class="pre">.ui</span></tt> file.</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>uifile</strong> &#8211; the file name or file-like object containing the <tt class="docutils literal"><span class="pre">.ui</span></tt> file.</li>
<li><strong>pyfile</strong> &#8211; the file-like object to which the generated Python code will be written
to.</li>
<li><strong>execute</strong> &#8211; is optionally set if a small amount of additional code is to be
generated that will display the user interface if the code is run as a
standalone application.</li>
<li><strong>indent</strong> &#8211; the optional number of spaces used for indentation in the generated
code.  If it is zero then a tab character is used instead.</li>
<li><strong>pyqt3_wrapper</strong> &#8211; is optionally set if a small wrapper is to be generated that allows the
generated code to be used as it is by PyQt v3 applications.</li>
<li><strong>from_imports</strong> &#8211; is optionally set to generate import statements that are relative to
<tt class="docutils literal"><span class="pre">'.'</span></tt>.  At the moment this only applies to the import of resource
modules.</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name" colspan="2">Resource_suffix :</th></tr>
<tr class="field-even field"><td>&nbsp;</td><td class="field-body"><p class="first last">is the suffix appended to the basename of any resource file specified
in the <tt class="docutils literal"><span class="pre">.ui</span></tt> file to create the name of the Python module generated
from the resource file by <tt class="docutils literal"><span class="pre">pyrcc4</span></tt>.  The default is <tt class="docutils literal"><span class="pre">'_rc'</span></tt>, i.e.
if the <tt class="docutils literal"><span class="pre">.ui</span></tt> file specified a resource file called <tt class="docutils literal"><span class="pre">foo.qrc</span></tt> then
the corresponding Python module is <tt class="docutils literal"><span class="pre">foo_rc</span></tt>.</p>
</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="function">
<dt id="PyQt4.uic.compileUiDir">
<tt class="descclassname">PyQt4.uic.</tt><tt class="descname">compileUiDir</tt><big>(</big><em>dir</em><span class="optional">[</span>, <em>recurse=False</em><span class="optional">[</span>, <em>map=None</em><span class="optional">[</span>, <em>**compileUi_args</em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#PyQt4.uic.compileUiDir" title="Permalink to this definition">¶</a></dt>
<dd><p>Create Python modules from Qt Designer <tt class="docutils literal"><span class="pre">.ui</span></tt> files in a directory or
directory tree.</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>dir</strong> &#8211; the name of the directory to scan for files whose name ends with
<tt class="docutils literal"><span class="pre">.ui</span></tt>.  By default the generated Python module is created in the same
directory ending with <tt class="docutils literal"><span class="pre">.py</span></tt>.</li>
<li><strong>recurse</strong> &#8211; is optionally set if any sub-directories should be scanned.</li>
<li><strong>map</strong> &#8211; an optional callable that is passed the name of the directory
containing the <tt class="docutils literal"><span class="pre">.ui</span></tt> file and the name of the Python module that will
be created.  The callable should return a tuple of the name of the
directory in which the Python module will be created and the (possibly
modified) name of the module.</li>
<li><strong>compileUi_args</strong> &#8211; are any additional keyword arguments that are passed to
<a class="reference internal" href="#PyQt4.uic.compileUi" title="PyQt4.uic.compileUi"><tt class="xref py py-func docutils literal"><span class="pre">compileUi()</span></tt></a> that is called to create each Python
module.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="function">
<dt id="PyQt4.uic.loadUiType">
<tt class="descclassname">PyQt4.uic.</tt><tt class="descname">loadUiType</tt><big>(</big><em>uifile</em><span class="optional">[</span>, <em>from_imports=False</em><span class="optional">[</span>, <em>resource_suffix='_rc'</em><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#PyQt4.uic.loadUiType" title="Permalink to this definition">¶</a></dt>
<dd><p>Load a Qt Designer <tt class="docutils literal"><span class="pre">.ui</span></tt> file and return a tuple of the generated
<em>form class</em> and the <em>Qt base class</em>.  These can then be used to
create any number of instances of the user interface without having to
parse the <tt class="docutils literal"><span class="pre">.ui</span></tt> file more than once.</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>uifile</strong> &#8211; the file name or file-like object containing the <tt class="docutils literal"><span class="pre">.ui</span></tt> file.</li>
<li><strong>from_imports</strong> &#8211; is optionally set to use import statements that are relative to
<tt class="docutils literal"><span class="pre">'.'</span></tt>.  At the moment this only applies to the import of resource
modules.</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name" colspan="2">Resource_suffix :</th></tr>
<tr class="field-even field"><td>&nbsp;</td><td class="field-body"><p class="first">is the suffix appended to the basename of any resource file specified
in the <tt class="docutils literal"><span class="pre">.ui</span></tt> file to create the name of the Python module generated
from the resource file by <tt class="docutils literal"><span class="pre">pyrcc4</span></tt>.  The default is <tt class="docutils literal"><span class="pre">'_rc'</span></tt>, i.e.
if the <tt class="docutils literal"><span class="pre">.ui</span></tt> file specified a resource file called <tt class="docutils literal"><span class="pre">foo.qrc</span></tt> then
the corresponding Python module is <tt class="docutils literal"><span class="pre">foo_rc</span></tt>.</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">Return type:</th><td class="field-body"><p class="first last">the <em>form class</em> and the <em>Qt base class</em>.</p>
</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="function">
<dt id="PyQt4.uic.loadUi">
<tt class="descclassname">PyQt4.uic.</tt><tt class="descname">loadUi</tt><big>(</big><em>uifile</em><span class="optional">[</span>, <em>baseinstance=None</em><span class="optional">[</span>, <em>package=''</em><span class="optional">[</span>, <em>resource_suffix='_rc'</em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#PyQt4.uic.loadUi" title="Permalink to this definition">¶</a></dt>
<dd><p>Load a Qt Designer <tt class="docutils literal"><span class="pre">.ui</span></tt> file and returns an instance of the user
interface.</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>uifile</strong> &#8211; the file name or file-like object containing the <tt class="docutils literal"><span class="pre">.ui</span></tt> file.</li>
<li><strong>baseinstance</strong> &#8211; the optional instance of the <em>Qt base class</em>.  If specified then the
user interface is created in it.  Otherwise a new instance of the base
class is automatically created.</li>
<li><strong>package</strong> &#8211; the optional package that is the base package for any relative imports
of custom widgets.</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name" colspan="2">Resource_suffix :</th></tr>
<tr class="field-even field"><td>&nbsp;</td><td class="field-body"><p class="first">is the suffix appended to the basename of any resource file specified
in the <tt class="docutils literal"><span class="pre">.ui</span></tt> file to create the name of the Python module generated
from the resource file by <tt class="docutils literal"><span class="pre">pyrcc4</span></tt>.  The default is <tt class="docutils literal"><span class="pre">'_rc'</span></tt>, i.e.
if the <tt class="docutils literal"><span class="pre">.ui</span></tt> file specified a resource file called <tt class="docutils literal"><span class="pre">foo.qrc</span></tt> then
the corresponding Python module is <tt class="docutils literal"><span class="pre">foo_rc</span></tt>.</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">Return type:</th><td class="field-body"><p class="first last">the <tt class="docutils literal"><span class="pre">QWidget</span></tt> sub-class that implements the user interface.</p>
</td>
</tr>
</tbody>
</table>
</dd></dl>

</div>
<div class="section" id="pyuic4">
<h2><strong class="program">pyuic4</strong><a class="headerlink" href="#pyuic4" title="Permalink to this headline">¶</a></h2>
<p>The <strong class="program">pyuic4</strong> utility is a command line interface to the
<a class="reference internal" href="#module-PyQt4.uic" title="PyQt4.uic"><tt class="xref py py-mod docutils literal"><span class="pre">uic</span></tt></a> module.  The command has the following syntax:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">pyuic4</span> <span class="p">[</span><span class="n">options</span><span class="p">]</span> <span class="o">.</span><span class="n">ui</span><span class="o">-</span><span class="nb">file</span>
</pre></div>
</div>
<p>The full set of command line options is:</p>
<dl class="cmdoption">
<dt id="cmdoption-pyuic4-h">
<tt class="descname">-h</tt><tt class="descclassname"></tt><tt class="descclassname">, </tt><tt class="descname">--help</tt><tt class="descclassname"></tt><a class="headerlink" href="#cmdoption-pyuic4-h" title="Permalink to this definition">¶</a></dt>
<dd><p>A help message is written to <tt class="docutils literal"><span class="pre">stdout</span></tt>.</p>
</dd></dl>

<dl class="cmdoption">
<dt id="cmdoption-pyuic4--version">
<tt class="descname">--version</tt><tt class="descclassname"></tt><a class="headerlink" href="#cmdoption-pyuic4--version" title="Permalink to this definition">¶</a></dt>
<dd><p>The version number is written to <tt class="docutils literal"><span class="pre">stdout</span></tt>.</p>
</dd></dl>

<dl class="cmdoption">
<dt id="cmdoption-pyuic4-i">
<tt class="descname">-i</tt><tt class="descclassname"> &lt;N&gt;</tt><tt class="descclassname">, </tt><tt class="descname">--indent</tt><tt class="descclassname"> &lt;N&gt;</tt><a class="headerlink" href="#cmdoption-pyuic4-i" title="Permalink to this definition">¶</a></dt>
<dd><p>The Python code is generated using an indentation of <tt class="docutils literal"><span class="pre">&lt;N&gt;</span></tt> spaces.  If
<tt class="docutils literal"><span class="pre">&lt;N&gt;</span></tt> is 0 then a tab is used.  The default is 4.</p>
</dd></dl>

<dl class="cmdoption">
<dt id="cmdoption-pyuic4-o">
<tt class="descname">-o</tt><tt class="descclassname"> &lt;FILE&gt;</tt><tt class="descclassname">, </tt><tt class="descname">--output</tt><tt class="descclassname"> &lt;FILE&gt;</tt><a class="headerlink" href="#cmdoption-pyuic4-o" title="Permalink to this definition">¶</a></dt>
<dd><p>The Python code generated is written to the file <tt class="docutils literal"><span class="pre">&lt;FILE&gt;</span></tt>.</p>
</dd></dl>

<dl class="cmdoption">
<dt id="cmdoption-pyuic4-p">
<tt class="descname">-p</tt><tt class="descclassname"></tt><tt class="descclassname">, </tt><tt class="descname">--preview</tt><tt class="descclassname"></tt><a class="headerlink" href="#cmdoption-pyuic4-p" title="Permalink to this definition">¶</a></dt>
<dd><p>The GUI is created dynamically and displayed.  No Python code is generated.</p>
</dd></dl>

<dl class="cmdoption">
<dt id="cmdoption-pyuic4-w">
<tt class="descname">-w</tt><tt class="descclassname"></tt><tt class="descclassname">, </tt><tt class="descname">--pyqt3-wrapper</tt><tt class="descclassname"></tt><a class="headerlink" href="#cmdoption-pyuic4-w" title="Permalink to this definition">¶</a></dt>
<dd><p>The generated Python code includes a small wrapper that allows the GUI to
be used in the same way as it is used in PyQt v3.</p>
</dd></dl>

<dl class="cmdoption">
<dt id="cmdoption-pyuic4-x">
<tt class="descname">-x</tt><tt class="descclassname"></tt><tt class="descclassname">, </tt><tt class="descname">--execute</tt><tt class="descclassname"></tt><a class="headerlink" href="#cmdoption-pyuic4-x" title="Permalink to this definition">¶</a></dt>
<dd><p>The generated Python code includes a small amount of additional code that
creates and displays the GUI when it is executes as a standalone
application.</p>
</dd></dl>

<dl class="cmdoption">
<dt id="cmdoption-pyuic4--from-imports">
<tt class="descname">--from-imports</tt><tt class="descclassname"></tt><a class="headerlink" href="#cmdoption-pyuic4--from-imports" title="Permalink to this definition">¶</a></dt>
<dd><p>Resource modules are imported using <tt class="docutils literal"><span class="pre">from</span> <span class="pre">.</span> <span class="pre">import</span></tt> rather than a simple
<tt class="docutils literal"><span class="pre">import</span></tt>.</p>
</dd></dl>

<dl class="cmdoption">
<dt id="cmdoption-pyuic4--resource-suffix">
<tt class="descname">--resource-suffix</tt><tt class="descclassname"> &lt;SUFFIX&gt;</tt><a class="headerlink" href="#cmdoption-pyuic4--resource-suffix" title="Permalink to this definition">¶</a></dt>
<dd><p>The suffix <tt class="docutils literal"><span class="pre">&lt;SUFFIX&gt;</span></tt> is appended to the basename of any resource file
specified in the <tt class="docutils literal"><span class="pre">.ui</span></tt> file to create the name of the Python module
generated from the resource file by <strong class="program">pyrcc4</strong>.  The default is
<tt class="docutils literal"><span class="pre">_rc</span></tt>.  For example if the <tt class="docutils literal"><span class="pre">.ui</span></tt> file specified a resource file called
<tt class="docutils literal"><span class="pre">foo.qrc</span></tt> then the corresponding Python module is <tt class="docutils literal"><span class="pre">foo_rc</span></tt>.</p>
</dd></dl>

<p>Note that code generated by <strong class="program">pyuic4</strong> is not guaranteed to be
compatible with earlier versions of PyQt4.  However, it is guaranteed to be
compatible with later versions.  If you have no control over the version of
PyQt4 the users of your application are using then you should run
<strong class="program">pyuic4</strong>, or call <a class="reference internal" href="#PyQt4.uic.compileUi" title="PyQt4.uic.compileUi"><tt class="xref py py-func docutils literal"><span class="pre">compileUi()</span></tt></a>, as part of your
installation process.  Another alternative would be to distribute the <tt class="docutils literal"><span class="pre">.ui</span></tt>
files (perhaps as part of a resource file) and have your application load them
dynamically.</p>
</div>
<div class="section" id="writing-qt-designer-plugins">
<span id="ref-designer-plugins"></span><h2>Writing Qt Designer Plugins<a class="headerlink" href="#writing-qt-designer-plugins" title="Permalink to this headline">¶</a></h2>
<p>Qt Designer can be extended by writing plugins.  Normally this is done using
C++ but PyQt4 also allows you to write plugins in Python.  Most of the time a
plugin is used to expose a custom widget to Designer so that it appears in
Designer&#8217;s widget box just like any other widget.  It is possibe to change the
widget&#8217;s properties and to connect its signals and slots.</p>
<p>It is also possible to add new functionality to Designer.  See the Qt
documentation for the full details.  Here we will concentrate on describing
how to write custom widgets in Python.</p>
<p>The process of integrating Python custom widgets with Designer is very similar
to that used with widget written using C++.  However, there are particular
issues that have to be addressed.</p>
<ul class="simple">
<li>Designer needs to have a C++ plugin that conforms to the interface defined by
the <tt class="docutils literal"><span class="pre">QDesignerCustomWidgetInterface</span></tt> class.  (If the plugin exposes more
than one custom widget then it must conform to the interface defined by the
<tt class="docutils literal"><span class="pre">QDesignerCustomWidgetCollectionInterface</span></tt> class.)  In addition the plugin
class must sub-class <tt class="docutils literal"><span class="pre">QObject</span></tt> as well as the interface class.  PyQt4 does
not allow Python classes to be sub-classed from more than one Qt class.</li>
<li>Designer can only connect Qt signals and slots.  It has no understanding of
Python signals or callables.</li>
<li>Designer can only edit Qt properties that represent C++ types.  It has no
understanding of Python attributes or Python types.</li>
</ul>
<p>PyQt4 provides the following components and features to resolve these issues as
simply as possible.</p>
<ul>
<li><p class="first">PyQt4&#8217;s QtDesigner module includes additional classes (all of which have a
<tt class="docutils literal"><span class="pre">QPy</span></tt> prefix) that are already sub-classed from the necessary Qt classes.
This avoids the need to sub-class from more than one Qt class in Python.  For
example, where a C++ custom widget plugin would sub-class from <tt class="docutils literal"><span class="pre">QObject</span></tt>
and <tt class="docutils literal"><span class="pre">QDesignerCustomWidgetInterface</span></tt>, a Python custom widget plugin would
instead sub-class from <tt class="docutils literal"><span class="pre">QPyDesignerCustomWidgetPlugin</span></tt>.</p>
</li>
<li><p class="first">PyQt4 installs a C++ plugin in Designer&#8217;s plugin directory.  It conforms to
the interface defined by the <tt class="docutils literal"><span class="pre">QDesignerCustomWidgetCollectionInterface</span></tt>
class.  It searches a configurable set of directories looking for Python
plugins that implement a class sub-classed from
<tt class="docutils literal"><span class="pre">QPyDesignerCustomWidgetPlugin</span></tt>.  Each class that is found is instantiated
and the instance created is added to the custom widget collection.</p>
<p>The <span class="target" id="index-0"></span><tt class="xref std std-envvar docutils literal"><span class="pre">PYQTDESIGNERPATH</span></tt> environment variable specifies the set of
directories to search for plugins.  Directory names are separated by a path
separator (a semi-colon on Windows and a colon on other platforms).  If a
directory name is empty (ie. there are consecutive path separators or a
leading or trailing path separator) then a set of default directories is
automatically inserted at that point.  The default directories are the
<tt class="file docutils literal"><span class="pre">python</span></tt> subdirectory of each directory that Designer searches for its
own plugins.  If the environment variable is not set then only the default
directories are searched.  If a file&#8217;s basename does not end with <tt class="docutils literal"><span class="pre">plugin</span></tt>
then it is ignored.</p>
</li>
<li><p class="first">A Python custom widget may define new Qt signals using
<a class="reference internal" href="new_style_signals_slots.html#PyQt4.QtCore.pyqtSignal" title="PyQt4.QtCore.pyqtSignal"><tt class="xref py py-func docutils literal"><span class="pre">pyqtSignal()</span></tt></a>.</p>
</li>
<li><p class="first">A Python method may be defined as a new Qt slot by using the
<a class="reference internal" href="new_style_signals_slots.html#PyQt4.QtCore.pyqtSlot" title="PyQt4.QtCore.pyqtSlot"><tt class="xref py py-func docutils literal"><span class="pre">pyqtSlot()</span></tt></a> decorator.</p>
</li>
<li><p class="first">A new Qt property may be defined using the <a class="reference internal" href="qt_properties.html#PyQt4.QtCore.pyqtProperty" title="PyQt4.QtCore.pyqtProperty"><tt class="xref py py-func docutils literal"><span class="pre">pyqtProperty()</span></tt></a>
function.</p>
</li>
</ul>
<p>Note that the ability to define new Qt signals, slots and properties from
Python is potentially useful to plugins conforming to any plugin interface and
not just that used by Designer.</p>
<p>For a simple but complete and fully documented example of a custom widget that
defines new Qt signals, slots and properties, and its plugin, look in the
<tt class="file docutils literal"><span class="pre">examples/designer/plugins</span></tt> directory of the PyQt4 source package.  The
<tt class="file docutils literal"><span class="pre">widgets</span></tt> subdirectory contains the <tt class="file docutils literal"><span class="pre">pydemo.py</span></tt> custom widget and
the <tt class="file docutils literal"><span class="pre">python</span></tt> subdirectory contains its <tt class="file docutils literal"><span class="pre">pydemoplugin.py</span></tt> plugin.</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="#">Using Qt Designer</a><ul>
<li><a class="reference internal" href="#using-the-generated-code">Using the Generated Code</a></li>
<li><a class="reference internal" href="#the-uic-module">The <tt class="docutils literal"><span class="pre">uic</span></tt> Module</a></li>
<li><a class="reference internal" href="#pyuic4"><strong class="program">pyuic4</strong></a></li>
<li><a class="reference internal" href="#writing-qt-designer-plugins">Writing Qt Designer Plugins</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="gotchas.html"
                        title="previous chapter">Things to be Aware Of</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="resources.html"
                        title="next chapter">The PyQt4 Resource System</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="resources.html" title="The PyQt4 Resource System"
             >next</a> |</li>
        <li class="right" >
          <a href="gotchas.html" title="Things to be Aware Of"
             >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>