1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
|
<!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 — PyQt 4.11.4 Reference Guide</title>
<link rel="stylesheet" href="_static/classic.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: './',
VERSION: '4.11.4',
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.11.4 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 role="document">
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="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 class="nav-item nav-item-0"><a href="index.html">PyQt 4.11.4 Reference Guide</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<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 <code class="docutils literal"><span class="pre">.ui</span></code> files to store designs and does not generate any
code itself. Qt includes the <code class="docutils literal"><span class="pre">uic</span></code> utility that generates the C++ code that
creates the user interface. Qt also includes the <code class="docutils literal"><span class="pre">QUiLoader</span></code> class that
allows an application to load a <code class="docutils literal"><span class="pre">.ui</span></code> file and to create the corresponding
user interface dynamically.</p>
<p>PyQt4 does not wrap the <code class="docutils literal"><span class="pre">QUiLoader</span></code> class but instead includes the
<a class="reference internal" href="#module-PyQt4.uic" title="PyQt4.uic"><code class="xref py py-mod docutils literal"><span class="pre">uic</span></code></a> Python module. Like <code class="docutils literal"><span class="pre">QUiLoader</span></code> this module can load
<code class="docutils literal"><span class="pre">.ui</span></code> 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’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"><code class="xref py py-mod docutils literal"><span class="pre">uic</span></code></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’s
<code class="docutils literal"><span class="pre">uic</span></code> and can be used in the same way.</p>
<p>The code is structured as a single class that is derived from the Python
<code class="docutils literal"><span class="pre">object</span></code> type. The name of the class is the name of the toplevel object set
in Designer with <code class="docutils literal"><span class="pre">Ui_</span></code> prepended. (In the C++ version the class is defined
in the <code class="docutils literal"><span class="pre">Ui</span></code> namespace.) We refer to this class as the <em>form class</em>.</p>
<p>The class contains a method called <code class="docutils literal"><span class="pre">setupUi()</span></code>. This takes a single argument
which is the widget in which the user interface is created. The type of this
argument (typically <code class="docutils literal"><span class="pre">QDialog</span></code>, <code class="docutils literal"><span class="pre">QWidget</span></code> or <code class="docutils literal"><span class="pre">QMainWindow</span></code>) 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 <code class="docutils literal"><span class="pre">.ui</span></code> file has been created
containing a dialog and the name of the <code class="docutils literal"><span class="pre">QDialog</span></code> object is <code class="docutils literal"><span class="pre">ImageDialog</span></code>.
We also assume that the name of the file containing the generated Python code
is <code class="file docutils literal"><span class="pre">ui_imagedialog.py</span></code>. 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
<code class="docutils literal"><span class="pre">QDialog</span></code> and set up the user interface in the <code class="docutils literal"><span class="pre">__init__()</span></code> 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">"2 colors (1 bit per pixel)"</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">"2 colors (1 bit per pixel)"</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">"2 colors (1 bit per pixel)"</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"><code class="xref py py-mod docutils literal"><span class="pre">uic</span></code></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"><code class="xref py py-mod docutils literal"><span class="pre">uic</span></code></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">
<code class="descclassname">PyQt4.uic.</code><code class="descname">widgetPluginPath</code><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">
<code class="descclassname">PyQt4.uic.</code><code class="descname">compileUi</code><span class="sig-paren">(</span><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><span class="sig-paren">)</span><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 <code class="docutils literal"><span class="pre">.ui</span></code> 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> – the file name or file-like object containing the <code class="docutils literal"><span class="pre">.ui</span></code> file.</li>
<li><strong>pyfile</strong> – the file-like object to which the generated Python code will be written
to.</li>
<li><strong>execute</strong> – 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> – 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> – 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> – is optionally set to generate import statements that are relative to
<code class="docutils literal"><span class="pre">'.'</span></code>. 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> </td><td class="field-body"><p class="first last">is the suffix appended to the basename of any resource file specified
in the <code class="docutils literal"><span class="pre">.ui</span></code> file to create the name of the Python module generated
from the resource file by <code class="docutils literal"><span class="pre">pyrcc4</span></code>. The default is <code class="docutils literal"><span class="pre">'_rc'</span></code>, i.e.
if the <code class="docutils literal"><span class="pre">.ui</span></code> file specified a resource file called <code class="docutils literal"><span class="pre">foo.qrc</span></code> then
the corresponding Python module is <code class="docutils literal"><span class="pre">foo_rc</span></code>.</p>
</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="function">
<dt id="PyQt4.uic.compileUiDir">
<code class="descclassname">PyQt4.uic.</code><code class="descname">compileUiDir</code><span class="sig-paren">(</span><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><span class="sig-paren">)</span><a class="headerlink" href="#PyQt4.uic.compileUiDir" title="Permalink to this definition">¶</a></dt>
<dd><p>Create Python modules from Qt Designer <code class="docutils literal"><span class="pre">.ui</span></code> 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> – the name of the directory to scan for files whose name ends with
<code class="docutils literal"><span class="pre">.ui</span></code>. By default the generated Python module is created in the same
directory ending with <code class="docutils literal"><span class="pre">.py</span></code>.</li>
<li><strong>recurse</strong> – is optionally set if any sub-directories should be scanned.</li>
<li><strong>map</strong> – an optional callable that is passed the name of the directory
containing the <code class="docutils literal"><span class="pre">.ui</span></code> 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> – are any additional keyword arguments that are passed to
<a class="reference internal" href="#PyQt4.uic.compileUi" title="PyQt4.uic.compileUi"><code class="xref py py-func docutils literal"><span class="pre">compileUi()</span></code></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">
<code class="descclassname">PyQt4.uic.</code><code class="descname">loadUiType</code><span class="sig-paren">(</span><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><span class="sig-paren">)</span><a class="headerlink" href="#PyQt4.uic.loadUiType" title="Permalink to this definition">¶</a></dt>
<dd><p>Load a Qt Designer <code class="docutils literal"><span class="pre">.ui</span></code> 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 <code class="docutils literal"><span class="pre">.ui</span></code> 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> – the file name or file-like object containing the <code class="docutils literal"><span class="pre">.ui</span></code> file.</li>
<li><strong>from_imports</strong> – is optionally set to use import statements that are relative to
<code class="docutils literal"><span class="pre">'.'</span></code>. 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> </td><td class="field-body"><p class="first">is the suffix appended to the basename of any resource file specified
in the <code class="docutils literal"><span class="pre">.ui</span></code> file to create the name of the Python module generated
from the resource file by <code class="docutils literal"><span class="pre">pyrcc4</span></code>. The default is <code class="docutils literal"><span class="pre">'_rc'</span></code>, i.e.
if the <code class="docutils literal"><span class="pre">.ui</span></code> file specified a resource file called <code class="docutils literal"><span class="pre">foo.qrc</span></code> then
the corresponding Python module is <code class="docutils literal"><span class="pre">foo_rc</span></code>.</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">
<code class="descclassname">PyQt4.uic.</code><code class="descname">loadUi</code><span class="sig-paren">(</span><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><span class="sig-paren">)</span><a class="headerlink" href="#PyQt4.uic.loadUi" title="Permalink to this definition">¶</a></dt>
<dd><p>Load a Qt Designer <code class="docutils literal"><span class="pre">.ui</span></code> 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> – the file name or file-like object containing the <code class="docutils literal"><span class="pre">.ui</span></code> file.</li>
<li><strong>baseinstance</strong> – 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> – 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> </td><td class="field-body"><p class="first">is the suffix appended to the basename of any resource file specified
in the <code class="docutils literal"><span class="pre">.ui</span></code> file to create the name of the Python module generated
from the resource file by <code class="docutils literal"><span class="pre">pyrcc4</span></code>. The default is <code class="docutils literal"><span class="pre">'_rc'</span></code>, i.e.
if the <code class="docutils literal"><span class="pre">.ui</span></code> file specified a resource file called <code class="docutils literal"><span class="pre">foo.qrc</span></code> then
the corresponding Python module is <code class="docutils literal"><span class="pre">foo_rc</span></code>.</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">Return type:</th><td class="field-body"><p class="first last">the <code class="docutils literal"><span class="pre">QWidget</span></code> 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"><code class="xref py py-mod docutils literal"><span class="pre">uic</span></code></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">
<span id="cmdoption-pyuic4--help"></span><code class="descname">-h</code><code class="descclassname"></code><code class="descclassname">, </code><code class="descname">--help</code><code class="descclassname"></code><a class="headerlink" href="#cmdoption-pyuic4-h" title="Permalink to this definition">¶</a></dt>
<dd><p>A help message is written to <code class="docutils literal"><span class="pre">stdout</span></code>.</p>
</dd></dl>
<dl class="cmdoption">
<dt id="cmdoption-pyuic4--version">
<code class="descname">--version</code><code class="descclassname"></code><a class="headerlink" href="#cmdoption-pyuic4--version" title="Permalink to this definition">¶</a></dt>
<dd><p>The version number is written to <code class="docutils literal"><span class="pre">stdout</span></code>.</p>
</dd></dl>
<dl class="cmdoption">
<dt id="cmdoption-pyuic4-i">
<span id="cmdoption-pyuic4--indent"></span><code class="descname">-i</code><code class="descclassname"> <N></code><code class="descclassname">, </code><code class="descname">--indent</code><code class="descclassname"> <N></code><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 <code class="docutils literal"><span class="pre"><N></span></code> spaces. If
<code class="docutils literal"><span class="pre"><N></span></code> is 0 then a tab is used. The default is 4.</p>
</dd></dl>
<dl class="cmdoption">
<dt id="cmdoption-pyuic4-o">
<span id="cmdoption-pyuic4--output"></span><code class="descname">-o</code><code class="descclassname"> <FILE></code><code class="descclassname">, </code><code class="descname">--output</code><code class="descclassname"> <FILE></code><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 <code class="docutils literal"><span class="pre"><FILE></span></code>.</p>
</dd></dl>
<dl class="cmdoption">
<dt id="cmdoption-pyuic4-p">
<span id="cmdoption-pyuic4--preview"></span><code class="descname">-p</code><code class="descclassname"></code><code class="descclassname">, </code><code class="descname">--preview</code><code class="descclassname"></code><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">
<span id="cmdoption-pyuic4--pyqt3-wrapper"></span><code class="descname">-w</code><code class="descclassname"></code><code class="descclassname">, </code><code class="descname">--pyqt3-wrapper</code><code class="descclassname"></code><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">
<span id="cmdoption-pyuic4--execute"></span><code class="descname">-x</code><code class="descclassname"></code><code class="descclassname">, </code><code class="descname">--execute</code><code class="descclassname"></code><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">
<code class="descname">--from-imports</code><code class="descclassname"></code><a class="headerlink" href="#cmdoption-pyuic4--from-imports" title="Permalink to this definition">¶</a></dt>
<dd><p>Resource modules are imported using <code class="docutils literal"><span class="pre">from</span> <span class="pre">.</span> <span class="pre">import</span></code> rather than a simple
<code class="docutils literal"><span class="pre">import</span></code>.</p>
</dd></dl>
<dl class="cmdoption">
<dt id="cmdoption-pyuic4--resource-suffix">
<code class="descname">--resource-suffix</code><code class="descclassname"> <SUFFIX></code><a class="headerlink" href="#cmdoption-pyuic4--resource-suffix" title="Permalink to this definition">¶</a></dt>
<dd><p>The suffix <code class="docutils literal"><span class="pre"><SUFFIX></span></code> is appended to the basename of any resource file
specified in the <code class="docutils literal"><span class="pre">.ui</span></code> file to create the name of the Python module
generated from the resource file by <strong class="program">pyrcc4</strong>. The default is
<code class="docutils literal"><span class="pre">_rc</span></code>. For example if the <code class="docutils literal"><span class="pre">.ui</span></code> file specified a resource file called
<code class="docutils literal"><span class="pre">foo.qrc</span></code> then the corresponding Python module is <code class="docutils literal"><span class="pre">foo_rc</span></code>.</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"><code class="xref py py-func docutils literal"><span class="pre">compileUi()</span></code></a>, as part of your
installation process. Another alternative would be to distribute the <code class="docutils literal"><span class="pre">.ui</span></code>
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’s widget box just like any other widget. It is possibe to change the
widget’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 <code class="docutils literal"><span class="pre">QDesignerCustomWidgetInterface</span></code> class. (If the plugin exposes more
than one custom widget then it must conform to the interface defined by the
<code class="docutils literal"><span class="pre">QDesignerCustomWidgetCollectionInterface</span></code> class.) In addition the plugin
class must sub-class <code class="docutils literal"><span class="pre">QObject</span></code> 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’s QtDesigner module includes additional classes (all of which have a
<code class="docutils literal"><span class="pre">QPy</span></code> 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 <code class="docutils literal"><span class="pre">QObject</span></code>
and <code class="docutils literal"><span class="pre">QDesignerCustomWidgetInterface</span></code>, a Python custom widget plugin would
instead sub-class from <code class="docutils literal"><span class="pre">QPyDesignerCustomWidgetPlugin</span></code>.</p>
</li>
<li><p class="first">PyQt4 installs a C++ plugin in Designer’s plugin directory. It conforms to
the interface defined by the <code class="docutils literal"><span class="pre">QDesignerCustomWidgetCollectionInterface</span></code>
class. It searches a configurable set of directories looking for Python
plugins that implement a class sub-classed from
<code class="docutils literal"><span class="pre">QPyDesignerCustomWidgetPlugin</span></code>. 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><code class="xref std std-envvar docutils literal"><span class="pre">PYQTDESIGNERPATH</span></code> 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
<code class="file docutils literal"><span class="pre">python</span></code> 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’s basename does not end with <code class="docutils literal"><span class="pre">plugin</span></code>
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"><code class="xref py py-func docutils literal"><span class="pre">pyqtSignal()</span></code></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"><code class="xref py py-func docutils literal"><span class="pre">pyqtSlot()</span></code></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"><code class="xref py py-func docutils literal"><span class="pre">pyqtProperty()</span></code></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
<code class="file docutils literal"><span class="pre">examples/designer/plugins</span></code> directory of the PyQt4 source package. The
<code class="file docutils literal"><span class="pre">widgets</span></code> subdirectory contains the <code class="file docutils literal"><span class="pre">pydemo.py</span></code> custom widget and
the <code class="file docutils literal"><span class="pre">python</span></code> subdirectory contains its <code class="file docutils literal"><span class="pre">pydemoplugin.py</span></code> plugin.</p>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<p class="logo"><a href="index.html">
<img class="logo" src="_static/logo.png" alt="Logo"/>
</a></p>
<h3><a href="index.html">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">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 <code class="docutils literal"><span class="pre">uic</span></code> 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" role="search">
<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" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="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 class="nav-item nav-item-0"><a href="index.html">PyQt 4.11.4 Reference Guide</a> »</li>
</ul>
</div>
<div class="footer" role="contentinfo">
© Copyright 2015 Riverbank Computing Limited.
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.3.1.
</div>
</body>
</html>
|