Sophie

Sophie

distrib > Fedora > 18 > x86_64 > by-pkgid > ff187cb994c94c614ecc64c5a8528b1b > files > 1489

qt-doc-4.8.5-10.fc18.noarch.rpm

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en_US" lang="en_US">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- designer-manual.qdoc -->
  <title>Qt 4.8: Using a Designer UI File in Your Application</title>
  <link rel="stylesheet" type="text/css" href="style/style.css" />
  <script src="scripts/jquery.js" type="text/javascript"></script>
  <script src="scripts/functions.js" type="text/javascript"></script>
  <link rel="stylesheet" type="text/css" href="style/superfish.css" />
  <link rel="stylesheet" type="text/css" href="style/narrow.css" />
  <!--[if IE]>
<meta name="MSSmartTagsPreventParsing" content="true">
<meta http-equiv="imagetoolbar" content="no">
<![endif]-->
<!--[if lt IE 7]>
<link rel="stylesheet" type="text/css" href="style/style_ie6.css">
<![endif]-->
<!--[if IE 7]>
<link rel="stylesheet" type="text/css" href="style/style_ie7.css">
<![endif]-->
<!--[if IE 8]>
<link rel="stylesheet" type="text/css" href="style/style_ie8.css">
<![endif]-->

<script src="scripts/superfish.js" type="text/javascript"></script>
<script src="scripts/narrow.js" type="text/javascript"></script>

</head>
<body class="" onload="CheckEmptyAndLoadList();">
 <div class="header" id="qtdocheader">
    <div class="content"> 
    <div id="nav-logo">
      <a href="index.html">Home</a></div>
    <a href="index.html" class="qtref"><span>Qt Reference Documentation</span></a>
    <div id="narrowsearch"></div>
    <div id="nav-topright">
      <ul>
        <li class="nav-topright-home"><a href="http://qt.digia.com/">Qt HOME</a></li>
        <li class="nav-topright-dev"><a href="http://qt-project.org/">DEV</a></li>
        <li class="nav-topright-doc nav-topright-doc-active"><a href="http://qt-project.org/doc/">
          DOC</a></li>
        <li class="nav-topright-blog"><a href="http://blog.qt.digia.com/">BLOG</a></li>
      </ul>
    </div>
    <div id="shortCut">
      <ul>
        <li class="shortCut-topleft-inactive"><span><a href="index.html">Qt 4.8</a></span></li>
        <li class="shortCut-topleft-active"><a href="http://qt-project.org/doc/">ALL VERSIONS        </a></li>
      </ul>
     </div>
 <ul class="sf-menu" id="narrowmenu"> 
             <li><a href="#">API Lookup</a> 
                 <ul> 
                     <li><a href="classes.html">Class index</a></li> 
           <li><a href="functions.html">Function index</a></li> 
           <li><a href="modules.html">Modules</a></li> 
           <li><a href="namespaces.html">Namespaces</a></li> 
           <li><a href="qtglobal.html">Global Declarations</a></li> 
           <li><a href="qdeclarativeelements.html">QML elements</a></li> 
             </ul> 
             </li> 
             <li><a href="#">Qt Topics</a> 
                 <ul> 
                        <li><a href="qt-basic-concepts.html">Programming with Qt</a></li>  
                        <li><a href="qtquick.html">Device UIs &amp; Qt Quick</a></li>  
                        <li><a href="qt-gui-concepts.html">UI Design with Qt</a></li>  
                        <li><a href="supported-platforms.html">Supported Platforms</a></li>  
                        <li><a href="technology-apis.html">Qt and Key Technologies</a></li>  
                        <li><a href="best-practices.html">How-To's and Best Practices</a></li>  
              </ul> 
                 </li> 
                 <li><a href="#">Examples</a> 
                     <ul> 
                       <li><a href="all-examples.html">Examples</a></li> 
                       <li><a href="tutorials.html">Tutorials</a></li> 
                       <li><a href="demos.html">Demos</a></li> 
                       <li><a href="qdeclarativeexamples.html">QML Examples</a></li> 
                </ul> 
                     </li> 
                 </ul> 
    </div>
  </div>
  <div class="wrapper">
    <div class="hd">
      <span></span>
    </div>
    <div class="bd group">
      <div class="sidebar">
        <div class="searchlabel">
          Search index:</div>
        <div class="search" id="sidebarsearch">
          <form id="qtdocsearch" action="" onsubmit="return false;">
            <fieldset>
              <input type="text" name="searchstring" id="pageType" value="" />
 <div id="resultdialog"> 
 <a href="#" id="resultclose">Close</a> 
 <p id="resultlinks" class="all"><a href="#" id="showallresults">All</a> | <a href="#" id="showapiresults">API</a> | <a href="#" id="showarticleresults">Articles</a> | <a href="#" id="showexampleresults">Examples</a></p> 
 <p id="searchcount" class="all"><span id="resultcount"></span><span id="apicount"></span><span id="articlecount"></span><span id="examplecount"></span>&nbsp;results:</p> 
 <ul id="resultlist" class="all"> 
 </ul> 
 </div> 
            </fieldset>
          </form>
        </div>
        <div class="box first bottombar" id="lookup">
          <h2 title="API Lookup"><span></span>
            API Lookup</h2>
          <div  id="list001" class="list">
          <ul id="ul001" >
              <li class="defaultLink"><a href="classes.html">Class index</a></li>
              <li class="defaultLink"><a href="functions.html">Function index</a></li>
              <li class="defaultLink"><a href="modules.html">Modules</a></li>
              <li class="defaultLink"><a href="namespaces.html">Namespaces</a></li>
              <li class="defaultLink"><a href="qtglobal.html">Global Declarations</a></li>
              <li class="defaultLink"><a href="qdeclarativeelements.html">QML elements</a></li>
            </ul> 
          </div>
        </div>
        <div class="box bottombar" id="topics">
          <h2 title="Qt Topics"><span></span>
            Qt Topics</h2>
          <div id="list002" class="list">
            <ul id="ul002" >
               <li class="defaultLink"><a href="qt-basic-concepts.html">Programming with Qt</a></li> 
               <li class="defaultLink"><a href="qtquick.html">Device UIs &amp; Qt Quick</a></li> 
               <li class="defaultLink"><a href="qt-gui-concepts.html">UI Design with Qt</a></li> 
               <li class="defaultLink"><a href="supported-platforms.html">Supported Platforms</a></li>  
               <li class="defaultLink"><a href="technology-apis.html">Qt and Key Technologies</a></li> 
               <li class="defaultLink"><a href="best-practices.html">How-To's and Best Practices</a></li> 
            </ul>  
          </div>
        </div>
        <div class="box" id="examples">
          <h2 title="Examples"><span></span>
            Examples</h2>
          <div id="list003" class="list">
        <ul id="ul003">
              <li class="defaultLink"><a href="all-examples.html">Examples</a></li>
              <li class="defaultLink"><a href="tutorials.html">Tutorials</a></li>
              <li class="defaultLink"><a href="demos.html">Demos</a></li>
              <li class="defaultLink"><a href="qdeclarativeexamples.html">QML Examples</a></li>
            </ul> 
          </div>
        </div>
      </div>
      <div class="wrap">
        <div class="toolbar">
          <div class="breadcrumb toolblock">
            <ul>
              <li class="first"><a href="index.html">Home</a></li>
              <!--  Breadcrumbs go here -->
<li>Using a Designer UI File in Your Application</li>
            </ul>
          </div>
          <div class="toolbuttons toolblock">
            <ul>
              <li id="smallA" class="t_button">A</li>
              <li id="medA" class="t_button active">A</li>
              <li id="bigA" class="t_button">A</li>
              <li id="print" class="t_button"><a href="javascript:this.print();">
                <span>Print</span></a></li>
            </ul>
        </div>
        </div>
        <div class="content mainContent">
  <link rel="prev" href="designer-stylesheet.html" />
  <link rel="next" href="designer-using-custom-widgets.html" />
<p class="naviNextPrevious headerNavi">
<a class="prevPage" href="designer-stylesheet.html">Using Stylesheets with Qt Designer</a>
<a class="nextPage" href="designer-using-custom-widgets.html">Using Custom Widgets with Qt Designer</a>
</p><p/>
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#compile-time-form-processing">Compile Time Form Processing</a></li>
<li class="level2"><a href="#the-direct-approach">The Direct Approach</a></li>
<li class="level2"><a href="#the-single-inheritance-approach">The Single Inheritance Approach</a></li>
<li class="level3"><a href="#using-a-member-variable">Using a Member Variable</a></li>
<li class="level3"><a href="#using-a-pointer-member-variable">Using a Pointer Member Variable</a></li>
<li class="level2"><a href="#the-multiple-inheritance-approach">The Multiple Inheritance Approach</a></li>
<li class="level2"><a href="#reacting-to-language-changes">Reacting to Language Changes</a></li>
<li class="level1"><a href="#run-time-form-processing">Run Time Form Processing</a></li>
<li class="level2"><a href="#the-uitools-approach">The UiTools Approach</a></li>
<li class="level1"><a href="#automatic-connections">Automatic Connections</a></li>
<li class="level2"><a href="#a-dialog-without-auto-connect">A Dialog Without Auto-Connect</a></li>
<li class="level2"><a href="#widgets-and-dialogs-with-auto-connect">Widgets and Dialogs with Auto-Connect</a></li>
</ul>
</div>
<h1 class="title">Using a Designer UI File in Your Application</h1>
<span class="subtitle"></span>
<!-- $$$designer-using-a-ui-file.html-description -->
<div class="descr"> <a name="details"></a>
<p>Qt Designer UI files represent the widget tree of the form in XML format. The forms can be processed:</p>
<ul>
<li><a href="#compile-time-form-processing">At compile time</a>, which means that forms are converted to C++ code that can be compiled.</li>
<li><a href="#run-time-form-processing">At runtime</a>, which means that forms are processed by the <a href="quiloader.html">QUiLoader</a> class that dynamically constructs the widget tree while parsing the XML file.</li>
</ul>
<a name="compile-time-form-processing"></a>
<h2>Compile Time Form Processing</h2>
<p>You create user interface components with <i>Qt Designer</i> and use Qt's integrated build tools, <a href="qmake-manual.html">qmake</a> and <a href="uic.html">uic</a>, to generate code for them when the application is built. The generated code contains the form's user interface object. It is a C++ struct that contains:</p>
<ul>
<li>Pointers to the form's widgets, layouts, layout items, button groups, and actions.</li>
<li>A member function called <tt>setupUi()</tt> to build the widget tree on the parent widget.</li>
<li>A member function called <tt>retranslateUi()</tt> that handles the translation of the string properties of the form. For more information, see <a href="#reacting-to-language-changes">Reacting to Language Changes</a>.</li>
</ul>
<p>The generated code can be included in your application and used directly from it. Alternatively, you can use it to extend subclasses of standard widgets.</p>
<p>A compile time processed form can be used in your application with one of the following approaches:</p>
<ul>
<li><a href="#the-direct-approach">The Direct Approach</a>: you construct a widget to use as a placeholder for the component, and set up the user interface inside it.</li>
<li><a href="#the-single-inheritance-approach">The Single Inheritance Approach</a>: you subclass the form's base class (<a href="qwidget.html">QWidget</a> or <a href="qdialog.html">QDialog</a>, for example), and include a private instance of the form's user interface object.</li>
<li><a href="#the-multiple-inheritance-approach">The Multiple Inheritance Approach</a>: you subclass both the form's base class and the form's user interface object. This allows the widgets defined in the form to be used directly from within the scope of the subclass.</li>
</ul>
<p>To demonstrate, we create a simple Calculator Form application. It is based on the original <a href="designer-calculatorform.html">Calculator Form</a> example.</p>
<p>The application consists of one source file, <tt>main.cpp</tt> and a UI file.</p>
<p>The <tt>calculatorform.ui</tt> file designed with <i>Qt Designer</i> is shown below:</p>
<p class="centerAlign"><img src="images/directapproach-calculatorform.png" alt="" /></p><p>We will use <tt>qmake</tt> to build the executable, so we need to write a <tt>.pro</tt> file:</p>
<pre class="cpp"> TEMPLATE    = app
 FORMS       = calculatorform.ui
 SOURCES     = main.cpp</pre>
<p>The special feature of this file is the <tt>FORMS</tt> declaration that tells <tt>qmake</tt> which files to process with <tt>uic</tt>. In this case, the <tt>calculatorform.ui</tt> file is used to create a <tt>ui_calculatorform.h</tt> file that can be used by any file listed in the <tt>SOURCES</tt> declaration.</p>
<p><b>Note:</b> You can use Qt Creator to create the Calculator Form project. It automatically generates the main.cpp, UI, and .pro files, which you can then modify.</p>
<a name="the-direct-approach"></a>
<h3>The Direct Approach</h3>
<p>To use the direct approach, we include the <tt>ui_calculatorform.h</tt> file directly in <tt>main.cpp</tt>:</p>
<pre class="cpp"> <span class="preprocessor">#include &quot;ui_calculatorform.h&quot;</span></pre>
<p>The <tt>main</tt> function creates the calculator widget by constructing a standard <a href="qwidget.html">QWidget</a> that we use to host the user interface described by the <tt>calculatorform.ui</tt> file.</p>
<pre class="cpp"> <span class="type">int</span> main(<span class="type">int</span> argc<span class="operator">,</span> <span class="type">char</span> <span class="operator">*</span>argv<span class="operator">[</span><span class="operator">]</span>)
 {
     <span class="type"><a href="qapplication.html">QApplication</a></span> app(argc<span class="operator">,</span> argv);
     <span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>widget <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qwidget.html">QWidget</a></span>;
     Ui<span class="operator">::</span>CalculatorForm ui;
     ui<span class="operator">.</span>setupUi(widget);

     widget<span class="operator">-</span><span class="operator">&gt;</span>show();
     <span class="keyword">return</span> app<span class="operator">.</span>exec();
 }</pre>
<p>In this case, the <tt>Ui::CalculatorForm</tt> is an interface description object from the <tt>ui_calculatorform.h</tt> file that sets up all the dialog's widgets and the connections between its signals and slots.</p>
<p>The direct approach provides a quick and easy way to use simple, self-contained components in your applications. However, componens created with <i>Qt Designer</i> often require close integration with the rest of the application code. For instance, the <tt>CalculatorForm</tt> code provided above will compile and run, but the <a href="qspinbox.html">QSpinBox</a> objects will not interact with the <a href="qlabel.html">QLabel</a> as we need a custom slot to carry out the add operation and display the result in the <a href="qlabel.html">QLabel</a>. To achieve this, we need to use the single inheritance approach.</p>
<a name="the-single-inheritance-approach"></a>
<h3>The Single Inheritance Approach</h3>
<p>To use the single inheritance approach, we subclass a standard Qt widget and include a private instance of the form's user interface object. This can take the form of:</p>
<ul>
<li>A member variable</li>
<li>A pointer member variable</li>
</ul>
<a name="using-a-member-variable"></a>
<h4>Using a Member Variable</h4>
<p>In this approach, we subclass a Qt widget and set up the user interface from within the constructor. Components used in this way expose the widgets and layouts used in the form to the Qt widget subclass, and provide a standard system for making signal and slot connections between the user interface and other objects in your application. The generated <tt>Ui::CalculatorForm</tt> structure is a member of the class.</p>
<p>This approach is used in the <a href="designer-calculatorform.html">Calculator Form</a> example.</p>
<p>To ensure that we can use the user interface, we need to include the header file that <tt>uic</tt> generates before referring to <tt>Ui::CalculatorForm</tt>:</p>
<pre class="cpp"> <span class="preprocessor">#include &quot;ui_calculatorform.h&quot;</span></pre>
<p>This means that the <tt>.pro</tt> file must be updated to include <tt>calculatorform.h</tt>:</p>
<pre class="cpp"> HEADERS     = calculatorform.h</pre>
<p>The subclass is defined in the following way:</p>
<pre class="cpp"> <span class="keyword">class</span> CalculatorForm : <span class="keyword">public</span> <span class="type"><a href="qwidget.html">QWidget</a></span>
 {
     Q_OBJECT

 <span class="keyword">public</span>:
     CalculatorForm(<span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>parent <span class="operator">=</span> <span class="number">0</span>);

 <span class="keyword">private</span> <span class="keyword">slots</span>:
     <span class="type">void</span> on_inputSpinBox1_valueChanged(<span class="type">int</span> value);
     <span class="type">void</span> on_inputSpinBox2_valueChanged(<span class="type">int</span> value);

 <span class="keyword">private</span>:
     Ui<span class="operator">::</span>CalculatorForm ui;
 };</pre>
<p>The important feature of the class is the private <tt>ui</tt> object which provides the code for setting up and managing the user interface.</p>
<p>The constructor for the subclass constructs and configures all the widgets and layouts for the dialog just by calling the <tt>ui</tt> object's <tt>setupUi()</tt> function. Once this has been done, it is possible to modify the user interface as needed.</p>
<pre class="cpp"> CalculatorForm<span class="operator">::</span>CalculatorForm(<span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>parent)
     : <span class="type"><a href="qwidget.html">QWidget</a></span>(parent)
 {
     ui<span class="operator">.</span>setupUi(<span class="keyword">this</span>);
 }</pre>
<p>We can connect signals and slots in user interface widgets in the usual way by adding the on_&lt;object name&gt; - prefix. For more information, see <a href="#widgets-and-dialogs-with-auto-connect">widgets-and-dialogs-with-auto-connect</a>.</p>
<p>The advantages of this approach are its simple use of inheritance to provide a <a href="qwidget.html">QWidget</a>-based interface, and its encapsulation of the user interface widget variables within the <tt>ui</tt> data member. We can use this method to define a number of user interfaces within the same widget, each of which is contained within its own namespace, and overlay (or compose) them. This approach can be used to create individual tabs from existing forms, for example.</p>
<a name="using-a-pointer-member-variable"></a>
<h4>Using a Pointer Member Variable</h4>
<p>Alternatively, the <tt>Ui::CalculatorForm</tt> structure can be made a pointer member of the class. The header then looks as follows:</p>
<pre class="cpp"> <span class="keyword">namespace</span> Ui {
     <span class="keyword">class</span> CalculatorForm;
 }

 <span class="keyword">class</span> CalculatorForm : <span class="keyword">public</span> <span class="type"><a href="qwidget.html">QWidget</a></span>
 <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>
 <span class="keyword">virtual</span> <span class="operator">~</span>CalculatorForm();
 <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>
 <span class="keyword">private</span>:
     Ui<span class="operator">::</span>CalculatorForm <span class="operator">*</span>ui;
 <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span></pre>
<p>The corresponding source file looks as follows:</p>
<pre class="cpp"> <span class="preprocessor">#include &quot;ui_calculatorform.h&quot;</span>

 CalculatorForm<span class="operator">::</span>CalculatorForm(<span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>parent) :
     <span class="type"><a href="qwidget.html">QWidget</a></span>(parent)<span class="operator">,</span> ui(<span class="keyword">new</span> Ui<span class="operator">::</span>CalculatorForm)
 {
     ui<span class="operator">-</span><span class="operator">&gt;</span>setupUi(<span class="keyword">this</span>);
 }

 CalculatorForm<span class="operator">::</span><span class="operator">~</span>CalculatorForm()
 {
     <span class="keyword">delete</span> ui;
 }</pre>
<p>The advantage of this approach is that the user interface object can be forward-declared, which means that we do not have to include the generated <tt>ui_calculatorform.h</tt> file in the header. The form can then be changed without recompiling the dependent source files. This is particularly important if the class is subject to binary compatibility restrictions.</p>
<p>We generally recommend this approach for libraries and large applications. For more information, see <a href="sharedlibrary.html">Creating Shared Libraries</a>.</p>
<a name="the-multiple-inheritance-approach"></a>
<h3>The Multiple Inheritance Approach</h3>
<p>Forms created with <i>Qt Designer</i> can be subclassed together with a standard <a href="qwidget.html">QWidget</a>-based class. This approach makes all the user interface components defined in the form directly accessible within the scope of the subclass, and enables signal and slot connections to be made in the usual way with the <a href="qobject.html#connect">connect()</a> function.</p>
<p>This approach is used in the <a href="uitools-multipleinheritance.html">Multiple Inheritance</a> example.</p>
<p>We need to include the header file that <tt>uic</tt> generates from the <tt>calculatorform.ui</tt> file, as follows:</p>
<pre class="cpp"> <span class="preprocessor">#include &quot;ui_calculatorform.h&quot;</span></pre>
<p>The class is defined in a similar way to the one used in the <a href="#the-single-inheritance-approach">single inheritance approach</a>, except that this time we inherit from <i>both</i> <a href="qwidget.html">QWidget</a> and <tt>Ui::CalculatorForm</tt>, as follows:</p>
<pre class="cpp"> <span class="keyword">class</span> CalculatorForm : <span class="keyword">public</span> <span class="type"><a href="qwidget.html">QWidget</a></span><span class="operator">,</span> <span class="keyword">private</span> Ui<span class="operator">::</span>CalculatorForm
 {
     Q_OBJECT

 <span class="keyword">public</span>:
     CalculatorForm(<span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>parent <span class="operator">=</span> <span class="number">0</span>);

 <span class="keyword">private</span> <span class="keyword">slots</span>:
     <span class="type">void</span> on_inputSpinBox1_valueChanged(<span class="type">int</span> value);
     <span class="type">void</span> on_inputSpinBox2_valueChanged(<span class="type">int</span> value);
 };</pre>
<p>We inherit <tt>Ui::CalculatorForm</tt> privately to ensure that the user interface objects are private in our subclass. We can also inherit it with the <tt>public</tt> or <tt>protected</tt> keywords in the same way that we could have made <tt>ui</tt> public or protected in the previous case.</p>
<p>The constructor for the subclass performs many of the same tasks as the constructor used in the <a href="#the-single-inheritance-approach">single inheritance</a> example:</p>
<pre class="cpp"> CalculatorForm<span class="operator">::</span>CalculatorForm(<span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>parent)
     : <span class="type"><a href="qwidget.html">QWidget</a></span>(parent)
 {
     setupUi(<span class="keyword">this</span>);
 }</pre>
<p>In this case, the widgets used in the user interface can be accessed in the same say as a widget created in code by hand. We no longer require the <tt>ui</tt> prefix to access them.</p>
<a name="reacting-to-language-changes"></a>
<h3>Reacting to Language Changes</h3>
<p>Qt notifies applications if the user interface language changes by sending an event of the type <a href="qevent.html#Type-enum">QEvent::LanguageChange</a>. To call the member function <tt>retranslateUi()</tt> of the user interface object, we reimplement <tt>QWidget::changeEvent()</tt> in the form class, as follows:</p>
<pre class="cpp"> <span class="type">void</span> CalculatorForm<span class="operator">::</span>changeEvent(<span class="type"><a href="qevent.html">QEvent</a></span> <span class="operator">*</span>e)
 {
     <span class="type"><a href="qwidget.html">QWidget</a></span><span class="operator">::</span>changeEvent(e);
     <span class="keyword">switch</span> (e<span class="operator">-</span><span class="operator">&gt;</span>type()) {
     <span class="keyword">case</span> <span class="type"><a href="qevent.html">QEvent</a></span><span class="operator">::</span>LanguageChange:
         ui<span class="operator">-</span><span class="operator">&gt;</span>retranslateUi(<span class="keyword">this</span>);
         <span class="keyword">break</span>;
     <span class="keyword">default</span>:
         <span class="keyword">break</span>;
    }
 }</pre>
<a name="run-time-form-processing"></a>
<h2>Run Time Form Processing</h2>
<p>Alternatively, forms can be processed at run time, producing dynamically- generated user interfaces. This can be done using the <a href="qtuitools.html">QtUiTools</a> module that provides the <a href="quiloader.html">QUiLoader</a> class to handle forms created with <i>Qt Designer</i>.</p>
<a name="the-uitools-approach"></a>
<h3>The UiTools Approach</h3>
<p>A resource file containing a UI file is required to process forms at run time. Also, the application needs to be configured to use the <a href="qtuitools.html">QtUiTools</a> module. This is done by including the following declaration in a <tt>qmake</tt> project file, ensuring that the application is compiled and linked appropriately.</p>
<pre class="cpp"> CONFIG += uitools</pre>
<p>The <a href="quiloader.html">QUiLoader</a> class provides a form loader object to construct the user interface. This user interface can be retrieved from any <a href="qiodevice.html">QIODevice</a>, e.g&#x2e;, a <a href="qfile.html">QFile</a> object, to obtain a form stored in a project's resource file. The <a href="quiloader.html#load">QUiLoader::load</a>() function constructs the form widget using the user interface description contained in the file.</p>
<p>The <a href="qtuitools.html">QtUiTools</a> module classes can be included using the following directive:</p>
<pre class="cpp"> <span class="preprocessor">#include &lt;QtUiTools&gt;</span></pre>
<p>The <a href="quiloader.html#load">QUiLoader::load</a>() function is invoked as shown in this code from the <a href="uitools-textfinder.html">Text Finder</a> example:</p>
<pre class="cpp"> <span class="type"><a href="qwidget.html">QWidget</a></span><span class="operator">*</span> TextFinder<span class="operator">::</span>loadUiFile()
 {
     <span class="type"><a href="quiloader.html">QUiLoader</a></span> loader;

     <span class="type"><a href="qfile.html">QFile</a></span> file(<span class="string">&quot;:/forms/textfinder.ui&quot;</span>);
     file<span class="operator">.</span>open(<span class="type"><a href="qfile.html">QFile</a></span><span class="operator">::</span>ReadOnly);

     <span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>formWidget <span class="operator">=</span> loader<span class="operator">.</span>load(<span class="operator">&amp;</span>file<span class="operator">,</span> <span class="keyword">this</span>);
     file<span class="operator">.</span>close();

     <span class="keyword">return</span> formWidget;
 }</pre>
<p>In a class that uses <a href="qtuitools.html">QtUiTools</a> to build its user interface at run time, we can locate objects in the form using qFindChild(). For example, in the follownig code, we locate some components based on their object names and widget types:</p>
<pre class="cpp">     ui_findButton <span class="operator">=</span> findChild<span class="operator">&lt;</span><span class="type"><a href="qpushbutton.html">QPushButton</a></span><span class="operator">*</span><span class="operator">&gt;</span>(<span class="string">&quot;findButton&quot;</span>);
     ui_textEdit <span class="operator">=</span> findChild<span class="operator">&lt;</span><span class="type"><a href="qtextedit.html">QTextEdit</a></span><span class="operator">*</span><span class="operator">&gt;</span>(<span class="string">&quot;textEdit&quot;</span>);
     ui_lineEdit <span class="operator">=</span> findChild<span class="operator">&lt;</span><span class="type"><a href="qlineedit.html">QLineEdit</a></span><span class="operator">*</span><span class="operator">&gt;</span>(<span class="string">&quot;lineEdit&quot;</span>);</pre>
<p>Processing forms at run-time gives the developer the freedom to change a program's user interface, just by changing the UI file. This is useful when customizing programs to suit various user needs, such as extra large icons or a different colour scheme for accessibility support.</p>
<a name="automatic-connections"></a>
<h2>Automatic Connections</h2>
<p>The signals and slots connections defined for compile time or run time forms can either be set up manually or automatically, using <a href="qmetaobject.html">QMetaObject</a>'s ability to make connections between signals and suitably-named slots.</p>
<p>Generally, in a <a href="qdialog.html">QDialog</a>, if we want to process the information entered by the user before accepting it, we need to connect the clicked() signal from the <b>OK</b> button to a custom slot in our dialog. We will first show an example of the dialog in which the slot is connected by hand then compare it with a dialog that uses automatic connection.</p>
<a name="a-dialog-without-auto-connect"></a>
<h3>A Dialog Without Auto-Connect</h3>
<p>We define the dialog in the same way as before, but now include a slot in addition to the constructor:</p>
<pre class="cpp"> <span class="keyword">class</span> ImageDialog : <span class="keyword">public</span> <span class="type"><a href="qdialog.html">QDialog</a></span><span class="operator">,</span> <span class="keyword">private</span> Ui<span class="operator">::</span>ImageDialog
 {
     Q_OBJECT

 <span class="keyword">public</span>:
     ImageDialog(<span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>parent <span class="operator">=</span> <span class="number">0</span>);

 <span class="keyword">private</span> <span class="keyword">slots</span>:
     <span class="type">void</span> checkValues();
 };</pre>
<p>The <tt>checkValues()</tt> slot will be used to validate the values provided by the user.</p>
<p>In the dialog's constructor we set up the widgets as before, and connect the <b>Cancel</b> button's <a href="qabstractbutton.html#clicked">clicked()</a> signal to the dialog's reject() slot. We also disable the <a href="qpushbutton.html#autoDefault-prop">autoDefault</a> property in both buttons to ensure that the dialog does not interfere with the way that the line edit handles return key events:</p>
<pre class="cpp"> ImageDialog<span class="operator">::</span>ImageDialog(<span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>parent)
     : <span class="type"><a href="qdialog.html">QDialog</a></span>(parent)
 {
     setupUi(<span class="keyword">this</span>);
     okButton<span class="operator">-</span><span class="operator">&gt;</span>setAutoDefault(<span class="keyword">false</span>);
     cancelButton<span class="operator">-</span><span class="operator">&gt;</span>setAutoDefault(<span class="keyword">false</span>);
     ...
     connect(okButton<span class="operator">,</span> SIGNAL(clicked())<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(checkValues()));
 }</pre>
<p>We connect the <b>OK</b> button's <a href="qabstractbutton.html#clicked">clicked()</a> signal to the dialog's checkValues() slot which we implement as follows:</p>
<pre class="cpp"> <span class="type">void</span> ImageDialog<span class="operator">::</span>checkValues()
 {
     <span class="keyword">if</span> (nameLineEdit<span class="operator">-</span><span class="operator">&gt;</span>text()<span class="operator">.</span>isEmpty())
         (<span class="type">void</span>) <span class="type"><a href="qmessagebox.html">QMessageBox</a></span><span class="operator">::</span>information(<span class="keyword">this</span><span class="operator">,</span> tr(<span class="string">&quot;No Image Name&quot;</span>)<span class="operator">,</span>
             tr(<span class="string">&quot;Please supply a name for the image.&quot;</span>)<span class="operator">,</span> <span class="type"><a href="qmessagebox.html">QMessageBox</a></span><span class="operator">::</span>Cancel);
     <span class="keyword">else</span>
         accept();
 }</pre>
<p>This custom slot does the minimum necessary to ensure that the data entered by the user is valid - it only accepts the input if a name was given for the image.</p>
<a name="widgets-and-dialogs-with-auto-connect"></a>
<h3>Widgets and Dialogs with Auto-Connect</h3>
<p>Although it is easy to implement a custom slot in the dialog and connect it in the constructor, we could instead use <a href="qmetaobject.html">QMetaObject</a>'s auto-connection facilities to connect the <b>OK</b> button's clicked() signal to a slot in our subclass. <tt>uic</tt> automatically generates code in the dialog's <tt>setupUi()</tt> function to do this, so we only need to declare and implement a slot with a name that follows a standard convention:</p>
<pre class="cpp"> <span class="type">void</span> on_<span class="operator">&lt;</span>object name<span class="operator">&gt;</span>_<span class="operator">&lt;</span>signal name<span class="operator">&gt;</span>(<span class="operator">&lt;</span>signal parameters<span class="operator">&gt;</span>);</pre>
<p>Using this convention, we can define and implement a slot that responds to mouse clicks on the <b>OK</b> button:</p>
<pre class="cpp"> <span class="keyword">class</span> ImageDialog : <span class="keyword">public</span> <span class="type"><a href="qdialog.html">QDialog</a></span><span class="operator">,</span> <span class="keyword">private</span> Ui<span class="operator">::</span>ImageDialog
 {
     Q_OBJECT

 <span class="keyword">public</span>:
     ImageDialog(<span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>parent <span class="operator">=</span> <span class="number">0</span>);

 <span class="keyword">private</span> <span class="keyword">slots</span>:
     <span class="type">void</span> on_okButton_clicked();
 };</pre>
<p>Another example of automatic signal and slot connection would be the <a href="uitools-textfinder.html">Text Finder</a> with its <tt>on_findButton_clicked()</tt> slot.</p>
<p>We use <a href="qmetaobject.html">QMetaObject</a>'s system to enable signal and slot connections:</p>
<pre class="cpp">     <span class="type"><a href="qmetaobject.html">QMetaObject</a></span><span class="operator">::</span>connectSlotsByName(<span class="keyword">this</span>);</pre>
<p>This enables us to implement the slot, as shown below:</p>
<pre class="cpp"> <span class="type">void</span> TextFinder<span class="operator">::</span>on_findButton_clicked()
 {
     <span class="type"><a href="qstring.html">QString</a></span> searchString <span class="operator">=</span> ui_lineEdit<span class="operator">-</span><span class="operator">&gt;</span>text();
     <span class="type"><a href="qtextdocument.html">QTextDocument</a></span> <span class="operator">*</span>document <span class="operator">=</span> ui_textEdit<span class="operator">-</span><span class="operator">&gt;</span>document();

     <span class="type">bool</span> found <span class="operator">=</span> <span class="keyword">false</span>;

     <span class="keyword">if</span> (isFirstTime <span class="operator">=</span><span class="operator">=</span> <span class="keyword">false</span>)
         document<span class="operator">-</span><span class="operator">&gt;</span>undo();

     <span class="keyword">if</span> (searchString<span class="operator">.</span>isEmpty()) {
         <span class="type"><a href="qmessagebox.html">QMessageBox</a></span><span class="operator">::</span>information(<span class="keyword">this</span><span class="operator">,</span> tr(<span class="string">&quot;Empty Search Field&quot;</span>)<span class="operator">,</span>
                 <span class="string">&quot;The search field is empty. Please enter a word and click Find.&quot;</span>);
     } <span class="keyword">else</span> {

         <span class="type"><a href="qtextcursor.html">QTextCursor</a></span> highlightCursor(document);
         <span class="type"><a href="qtextcursor.html">QTextCursor</a></span> cursor(document);

         cursor<span class="operator">.</span>beginEditBlock();
     ...
         cursor<span class="operator">.</span>endEditBlock();
         isFirstTime <span class="operator">=</span> <span class="keyword">false</span>;

         <span class="keyword">if</span> (found <span class="operator">=</span><span class="operator">=</span> <span class="keyword">false</span>) {
             <span class="type"><a href="qmessagebox.html">QMessageBox</a></span><span class="operator">::</span>information(<span class="keyword">this</span><span class="operator">,</span> tr(<span class="string">&quot;Word Not Found&quot;</span>)<span class="operator">,</span>
                 <span class="string">&quot;Sorry, the word cannot be found.&quot;</span>);
         }
     }
 }</pre>
<p>Automatic connection of signals and slots provides both a standard naming convention and an explicit interface for widget designers to work to. By providing source code that implements a given interface, user interface designers can check that their designs actually work without having to write code themselves.</p>
</div>
<!-- @@@designer-using-a-ui-file.html -->
<p class="naviNextPrevious footerNavi">
<a class="prevPage" href="designer-stylesheet.html">Using Stylesheets with Qt Designer</a>
<a class="nextPage" href="designer-using-custom-widgets.html">Using Custom Widgets with Qt Designer</a>
</p>
      </div>
    </div>
    </div> 
    <div class="ft">
      <span></span>
    </div>
  </div> 
  <div class="footer">
    <p>
      <acronym title="Copyright">&copy;</acronym> 2013 Digia Plc and/or its
      subsidiaries. Documentation contributions included herein are the copyrights of
      their respective owners.</p>
    <br />
    <p>
      The documentation provided herein is licensed under the terms of the
      <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation
      License version 1.3</a> as published by the Free Software Foundation.</p>
    <p>
      Documentation sources may be obtained from <a href="http://www.qt-project.org">
      www.qt-project.org</a>.</p>
    <br />
    <p>
      Digia, Qt and their respective logos are trademarks of Digia Plc 
      in Finland and/or other countries worldwide. All other trademarks are property
      of their respective owners. <a title="Privacy Policy"
      href="http://en.gitorious.org/privacy_policy/">Privacy Policy</a></p>
  </div>

  <script src="scripts/functions.js" type="text/javascript"></script>
</body>
</html>