This file is indexed.

/usr/share/qt5/doc/qtdesigner/designer-using-a-ui-file.html is in qttools5-doc-html 5.5.1-3build1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- designer-manual.qdoc -->
  <title>Using a Designer UI File in Your Application | Qt Designer Manual </title>
  <link rel="stylesheet" type="text/css" href="style/offline.css" />
</head>
<body>
<div class="header" id="qtdocheader">
    <div class="main">
    <div class="main-rounded">
        <div class="navigationbar">
        <ul>
<li>Qt 5.5</li>
<li><a href="qtdesigner-manual.html">Qt Designer Manual</a></li>
<li>Using a Designer UI File in Your Application</li>
<li id="buildversion">Qt 5.5.1 Reference Documentation</li>
    </ul>
    </div>
</div>
<div class="content">
<div class="line">
<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="sidebar">
<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="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>
<div class="sidebar-content" id="sidebar-content"></div></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="designer-using-a-ui-file.html#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="designer-using-a-ui-file.html#run-time-form-processing">At runtime</a>, which means that forms are processed by the QUiLoader class that dynamically constructs the widget tree while parsing the XML file.</li>
</ul>
<a name="compile-time-form-processing"></a>
<h2 id="compile-time-form-processing">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/qmake-manual.html">qmake</a> and uic, 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 <code>setupUi()</code> to build the widget tree on the parent widget.</li>
<li>A member function called <code>retranslateUi()</code> that handles the translation of the string properties of the form. For more information, see <a href="designer-using-a-ui-file.html#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="designer-using-a-ui-file.html#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="designer-using-a-ui-file.html#the-single-inheritance-approach">The Single Inheritance Approach</a>: you subclass the form's base class (<a href="../qtwidgets/qwidget.html">QWidget</a> or <a href="../qtwidgets/qdialog.html">QDialog</a>, for example), and include a private instance of the form's user interface object.</li>
<li><a href="designer-using-a-ui-file.html#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="qtdesigner-calculatorform-example.html">Calculator Form</a> example.</p>
<p>The application consists of one source file, <code>main.cpp</code> and a UI file.</p>
<p>The <code>calculatorform.ui</code> 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 <code>qmake</code> to build the executable, so we need to write a <code>.pro</code> file:</p>
<pre class="cpp">HEADERS     = calculatorform.h</pre>
<p>The special feature of this file is the <code>FORMS</code> declaration that tells <code>qmake</code> which files to process with <code>uic</code>. In this case, the <code>calculatorform.ui</code> file is used to create a <code>ui_calculatorform.h</code> file that can be used by any file listed in the <code>SOURCES</code> 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 <code>ui_calculatorform.h</code> file directly in <code>main.cpp</code>:</p>
<pre class="cpp"><span class="preprocessor">#include &quot;ui_calculatorform.h&quot;</span></pre>
<p>The <code>main</code> function creates the calculator widget by constructing a standard <a href="../qtwidgets/qwidget.html">QWidget</a> that we use to host the user interface described by the <code>calculatorform.ui</code> 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="../qtwidgets/qapplication.html">QApplication</a></span> app(argc<span class="operator">,</span> argv);
    <span class="type"><a href="../qtwidgets/qwidget.html">QWidget</a></span> <span class="operator">*</span>widget <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="../qtwidgets/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 <code>Ui::CalculatorForm</code> is an interface description object from the <code>ui_calculatorform.h</code> 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 <code>CalculatorForm</code> code provided above will compile and run, but the <a href="../qtwidgets/qspinbox.html">QSpinBox</a> objects will not interact with the <a href="../qtwidgets/qlabel.html">QLabel</a> as we need a custom slot to carry out the add operation and display the result in the <a href="../qtwidgets/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 <code>Ui::CalculatorForm</code> structure is a member of the class.</p>
<p>This approach is used in the <a href="qtdesigner-calculatorform-example.html">Calculator Form</a> example.</p>
<p>To ensure that we can use the user interface, we need to include the header file that <code>uic</code> generates before referring to <code>Ui::CalculatorForm</code>:</p>
<pre class="cpp"><span class="preprocessor">#include &quot;ui_calculatorform.h&quot;</span></pre>
<p>This means that the <code>.pro</code> file must be updated to include <code>calculatorform.h</code>:</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="../qtwidgets/qwidget.html">QWidget</a></span>
{
    Q_OBJECT

<span class="keyword">public</span>:
    CalculatorForm(<span class="type"><a href="../qtwidgets/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 <code>ui</code> 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 <code>ui</code> object's <code>setupUi()</code> 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="../qtwidgets/qwidget.html">QWidget</a></span> <span class="operator">*</span>parent)
    : <span class="type"><a href="../qtwidgets/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="designer-using-a-ui-file.html#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="../qtwidgets/qwidget.html">QWidget</a>-based interface, and its encapsulation of the user interface widget variables within the <code>ui</code> 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 <code>Ui::CalculatorForm</code> 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="../qtwidgets/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="../qtwidgets/qwidget.html">QWidget</a></span> <span class="operator">*</span>parent) :
    <span class="type"><a href="../qtwidgets/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 <code>ui_calculatorform.h</code> 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 Creating Shared Libraries.</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="../qtwidgets/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="../qtcore/qobject.html#connect">connect()</a> function.</p>
<p>This approach is used in the Multiple Inheritance example.</p>
<p>We need to include the header file that <code>uic</code> generates from the <code>calculatorform.ui</code> 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="designer-using-a-ui-file.html#the-single-inheritance-approach">single inheritance approach</a>, except that this time we inherit from <i>both</i> <a href="../qtwidgets/qwidget.html">QWidget</a> and <code>Ui::CalculatorForm</code>, as follows:</p>
<pre class="cpp"><span class="keyword">class</span> CalculatorForm : <span class="keyword">public</span> <span class="type"><a href="../qtwidgets/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="../qtwidgets/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 <code>Ui::CalculatorForm</code> privately to ensure that the user interface objects are private in our subclass. We can also inherit it with the <code>public</code> or <code>protected</code> keywords in the same way that we could have made <code>ui</code> 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="designer-using-a-ui-file.html#the-single-inheritance-approach">single inheritance</a> example:</p>
<pre class="cpp">CalculatorForm<span class="operator">::</span>CalculatorForm(<span class="type"><a href="../qtwidgets/qwidget.html">QWidget</a></span> <span class="operator">*</span>parent)
    : <span class="type"><a href="../qtwidgets/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 <code>ui</code> 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="../qtcore/qevent.html#Type-enum">QEvent::LanguageChange</a>. To call the member function <code>retranslateUi()</code> of the user interface object, we reimplement <code>QWidget::changeEvent()</code> 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="../qtcore/qevent.html">QEvent</a></span> <span class="operator">*</span>e)
{
    <span class="type"><a href="../qtwidgets/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="../qtcore/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 id="run-time-form-processing">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 QtUiTools module that provides the QUiLoader 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 QtUiTools module. This is done by including the following declaration in a <code>qmake</code> project file, ensuring that the application is compiled and linked appropriately.</p>
<pre class="cpp">QT += uitools</pre>
<p>The QUiLoader class provides a form loader object to construct the user interface. This user interface can be retrieved from any <a href="../qtcore/qiodevice.html">QIODevice</a>, e.g&#x2e;, a <a href="../qtcore/qfile.html">QFile</a> object, to obtain a form stored in a project's resource file. The QUiLoader::load() function constructs the form widget using the user interface description contained in the file.</p>
<p>The QtUiTools module classes can be included using the following directive:</p>
<pre class="cpp"><span class="preprocessor">#include &lt;QtUiTools&gt;</span></pre>
<p>The QUiLoader::load() function is invoked as shown in this code from the Text Finder example:</p>
<pre class="cpp"><span class="type"><a href="../qtwidgets/qwidget.html">QWidget</a></span><span class="operator">*</span> TextFinder<span class="operator">::</span>loadUiFile()
{
    <span class="type">QUiLoader</span> loader;

    <span class="type"><a href="../qtcore/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="../qtcore/qfile.html">QFile</a></span><span class="operator">::</span>ReadOnly);

    <span class="type"><a href="../qtwidgets/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 QtUiTools 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="../qtwidgets/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="../qtwidgets/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="../qtwidgets/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 id="automatic-connections">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="../qtcore/qmetaobject.html">QMetaObject</a>'s ability to make connections between signals and suitably-named slots.</p>
<p>Generally, in a <a href="../qtwidgets/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="../qtwidgets/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="../qtwidgets/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 <code>checkValues()</code> 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="../qtwidgets/qabstractbutton.html#clicked">clicked()</a> signal to the dialog's reject() slot. We also disable the <a href="../qtwidgets/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="../qtwidgets/qwidget.html">QWidget</a></span> <span class="operator">*</span>parent)
    : <span class="type"><a href="../qtwidgets/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="../qtwidgets/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="../qtwidgets/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="../qtwidgets/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="../qtcore/qmetaobject.html">QMetaObject</a>'s auto-connection facilities to connect the <b>OK</b> button's clicked() signal to a slot in our subclass. <code>uic</code> automatically generates code in the dialog's <code>setupUi()</code> 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="../qtwidgets/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="../qtwidgets/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 Text Finder with its <code>on_findButton_clicked()</code> slot.</p>
<p>We use <a href="../qtcore/qmetaobject.html">QMetaObject</a>'s system to enable signal and slot connections:</p>
<pre class="cpp">    <span class="type"><a href="../qtcore/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="../qtcore/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">QTextDocument</span> <span class="operator">*</span>document <span class="operator">=</span> ui_textEdit<span class="operator">-</span><span class="operator">&gt;</span>document();

    bool 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="../qtwidgets/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">QTextCursor</span> highlightCursor(document);
        <span class="type">QTextCursor</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="../qtwidgets/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>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2015 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br>    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.<br>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>