http://wiki.crossplatform.ru/index.php?title=Qt:%D0%94%D0%BE%D0%BA%D1%83%D0%BC%D0%B5%D0%BD%D1%82%D0%B0%D1%86%D0%B8%D1%8F_4.3.2/designer-using-a-component&feed=atom&action=historyQt:Документация 4.3.2/designer-using-a-component - История изменений2024-03-29T13:27:46ZИстория изменений этой страницы в викиMediaWiki 1.15.1http://wiki.crossplatform.ru/index.php?title=Qt:%D0%94%D0%BE%D0%BA%D1%83%D0%BC%D0%B5%D0%BD%D1%82%D0%B0%D1%86%D0%B8%D1%8F_4.3.2/designer-using-a-component&diff=2033&oldid=prevRoot в 10:10, 6 ноября 20082008-11-06T10:10:57Z<p></p>
<table style="background-color: white; color:black;">
<col class='diff-marker' />
<col class='diff-content' />
<col class='diff-marker' />
<col class='diff-content' />
<tr valign='top'>
<td colspan='2' style="background-color: white; color:black;">← Предыдущая</td>
<td colspan='2' style="background-color: white; color:black;">Версия 10:10, 6 ноября 2008</td>
</tr>
<tr><td colspan="2" class="diff-lineno">Строка 4:</td>
<td colspan="2" class="diff-lineno">Строка 4:</td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>=Using a Component in Your Application<br />=</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>=Using a Component in Your Application<br />=</div></td></tr>
<tr><td class='diff-marker'>-</td><td style="background: #ffa; color:black; font-size: smaller;"><div>*[[#<del class="diffchange diffchange-inline">Compile</del>-<del class="diffchange diffchange-inline">Time</del>-<del class="diffchange diffchange-inline">Form</del>-<del class="diffchange diffchange-inline">Processing </del>| Compile Time Form Processing]]</div></td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div>*[[#<ins class="diffchange diffchange-inline">compile</ins>-<ins class="diffchange diffchange-inline">time</ins>-<ins class="diffchange diffchange-inline">form</ins>-<ins class="diffchange diffchange-inline">processing </ins>| Compile Time Form Processing]]</div></td></tr>
<tr><td class='diff-marker'>-</td><td style="background: #ffa; color:black; font-size: smaller;"><div>**[[#<del class="diffchange diffchange-inline">The</del>-<del class="diffchange diffchange-inline">Direct</del>-<del class="diffchange diffchange-inline">Approach </del>| The Direct Approach]]</div></td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div>**[[#<ins class="diffchange diffchange-inline">the</ins>-<ins class="diffchange diffchange-inline">direct</ins>-<ins class="diffchange diffchange-inline">approach </ins>| The Direct Approach]]</div></td></tr>
<tr><td class='diff-marker'>-</td><td style="background: #ffa; color:black; font-size: smaller;"><div>**[[#<del class="diffchange diffchange-inline">The</del>-<del class="diffchange diffchange-inline">Single</del>-<del class="diffchange diffchange-inline">Inheritance</del>-<del class="diffchange diffchange-inline">Approach </del>| The Single Inheritance Approach]]</div></td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div>**[[#<ins class="diffchange diffchange-inline">the</ins>-<ins class="diffchange diffchange-inline">single</ins>-<ins class="diffchange diffchange-inline">inheritance</ins>-<ins class="diffchange diffchange-inline">approach </ins>| The Single Inheritance Approach]]</div></td></tr>
<tr><td class='diff-marker'>-</td><td style="background: #ffa; color:black; font-size: smaller;"><div>**[[#<del class="diffchange diffchange-inline">The</del>-<del class="diffchange diffchange-inline">Multiple</del>-<del class="diffchange diffchange-inline">Inheritance</del>-<del class="diffchange diffchange-inline">Approach </del>| The Multiple Inheritance Approach<del class="diffchange diffchange-inline">]]</del></div></td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div>**[[#<ins class="diffchange diffchange-inline">the</ins>-<ins class="diffchange diffchange-inline">multiple</ins>-<ins class="diffchange diffchange-inline">inheritance</ins>-<ins class="diffchange diffchange-inline">approach </ins>| The Multiple Inheritance Approach]]</div></td></tr>
<tr><td class='diff-marker'>-</td><td style="background: #ffa; color:black; font-size: smaller;"><div><del class="diffchange diffchange-inline">*[[#Run-Time-Form-Processing | Run Time Form Processing]]</del></div></td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div></div></td></tr>
<tr><td class='diff-marker'>-</td><td style="background: #ffa; color:black; font-size: smaller;"><div><del class="diffchange diffchange-inline">**[[#The-Uitools-Approach | The UiTools Approach]]</del></div></td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div></div></td></tr>
<tr><td class='diff-marker'>-</td><td style="background: #ffa; color:black; font-size: smaller;"><div><del class="diffchange diffchange-inline">*[[#Automatic-Connections | Automatic Connections]]</del></div></td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div></div></td></tr>
<tr><td class='diff-marker'>-</td><td style="background: #ffa; color:black; font-size: smaller;"><div><del class="diffchange diffchange-inline">**[[#A-Dialog-Without-Auto-Connect | A Dialog Without Auto-Connect]]</del></div></td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div></div></td></tr>
<tr><td class='diff-marker'>-</td><td style="background: #ffa; color:black; font-size: smaller;"><div><del class="diffchange diffchange-inline">**[[#Widgets-and-Dialogs-with-Auto-Connect | Widgets and Dialogs with Auto-Connect</del>]]</div></td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div></div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div><ins style="color: red; font-weight: bold; text-decoration: none;">*[[#run-time-form-processing | Run Time Form Processing]]</ins></div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div><ins style="color: red; font-weight: bold; text-decoration: none;">**[[#the-uitools-approach | The UiTools Approach]]</ins></div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"></td></tr>
<tr><td class='diff-marker'>-</td><td style="background: #ffa; color:black; font-size: smaller;"><div><del class="diffchange diffchange-inline">With Qt's integrated build tools, </del>[[<del class="diffchange diffchange-inline">Qt:Документация 4.3.2/qmake</del>-<del class="diffchange diffchange-inline">manual </del>| <del class="diffchange diffchange-inline">qmake</del>]] <del class="diffchange diffchange-inline">and </del>[[<del class="diffchange diffchange-inline">Qt:Документация 4.3.2/uic</del>#<del class="diffchange diffchange-inline">uic </del>| <del class="diffchange diffchange-inline">uic</del>]]<del class="diffchange diffchange-inline">, the code for user interface components created </del>with <del class="diffchange diffchange-inline">''Qt Designer'' is automatically generated when the rest of of your application is built. Forms can be included </del>and <del class="diffchange diffchange-inline">used directly from your application, or you can use them to extend subclasses of standard widgets. These forms can be processed at compile time or at run time, depending on the approach used.</del></div></td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div><ins class="diffchange diffchange-inline">*</ins>[[<ins class="diffchange diffchange-inline">#automatic</ins>-<ins class="diffchange diffchange-inline">connections </ins>| <ins class="diffchange diffchange-inline">Automatic Connections</ins>]]</div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div><ins class="diffchange diffchange-inline">**</ins>[[#<ins class="diffchange diffchange-inline">a-dialog-without-auto-connect </ins>| <ins class="diffchange diffchange-inline">A Dialog Without Auto-Connect</ins>]]</div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div><ins class="diffchange diffchange-inline">**[[#widgets-and-dialogs-</ins>with<ins class="diffchange diffchange-inline">-auto-connect | Widgets </ins>and <ins class="diffchange diffchange-inline">Dialogs with Auto-Connect]]</ins></div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div><ins style="color: red; font-weight: bold; text-decoration: none;"></ins></div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div><ins style="color: red; font-weight: bold; text-decoration: none;">With Qt's integrated build tools, [[Qt:Документация 4.3.2/qmake-manual | qmake]] and [[Qt:Документация 4.3.2/uic#uic | uic]], the code for user interface components created with ''Qt Designer'' is automatically generated when the rest of of your application is built. Forms can be included and used directly from your application, or you can use them to extend subclasses of standard widgets. These forms can be processed at compile time or at run time, depending on the approach used.</ins></div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div><ins style="color: red; font-weight: bold; text-decoration: none;"><div id="compiletimeformprocessing"></div><div id="compile-time-form-processing"></div></ins></div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>==Compile Time Form Processing==</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>==Compile Time Form Processing==</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>A compile time processed form can be used in your application with any of the following approaches:</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>A compile time processed form can be used in your application with any of the following approaches:</div></td></tr>
<tr><td colspan="2" class="diff-lineno">Строка 23:</td>
<td colspan="2" class="diff-lineno">Строка 25:</td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>*In the single inheritance approach, you subclass the form's base class ([[Qt:Документация 4.3.2/qwidget | QWidget]] or [[Qt:Документация 4.3.2/qdialog | QDialog]], for example), and include a private instance of the form's user interface object.</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>*In the single inheritance approach, you subclass the form's base class ([[Qt:Документация 4.3.2/qwidget | QWidget]] or [[Qt:Документация 4.3.2/qdialog | QDialog]], for example), and include a private instance of the form's user interface object.</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>*In the multiple inheritance approach, you subclass from 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.</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>*In the multiple inheritance approach, you subclass from 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.</div></td></tr>
<tr><td class='diff-marker'>-</td><td style="background: #ffa; color:black; font-size: smaller;"><div> </div></td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div><ins class="diffchange diffchange-inline"><div id="thedirectapproach"></div><div id="the-direct-approach"></div></ins></div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>===The Direct Approach===</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>===The Direct Approach===</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>To demonstrate how user interface components can be used straight from ''Qt Designer'', we create a simple Calculator Form application, based on the original [[Qt:Документация 4.3.2/designer-calculatorform | Calculator Form]] example.</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>To demonstrate how user interface components can be used straight from ''Qt Designer'', we create a simple Calculator Form application, based on the original [[Qt:Документация 4.3.2/designer-calculatorform | Calculator Form]] example.</div></td></tr>
<tr><td colspan="2" class="diff-lineno">Строка 55:</td>
<td colspan="2" class="diff-lineno">Строка 57:</td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>This approach provides a quick and easy way to use simple, self-contained components in your applications, but many components created with ''Qt Designer'' will need to be integrated more closely with the rest of the application code. For instance, the <tt>CalculatorForm</tt> code provided above will compile and run, but the [[Qt:Документация 4.3.2/qspinbox | QSpinBox]] objects will not interact with the [[Qt:Документация 4.3.2/qlabel | QLabel]] as we require a custom slot to carry out the add operation and display the result in the [[Qt:Документация 4.3.2/qlabel | QLabel]]. To achieve this, we need to subclass a standard Qt widget.</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>This approach provides a quick and easy way to use simple, self-contained components in your applications, but many components created with ''Qt Designer'' will need to be integrated more closely with the rest of the application code. For instance, the <tt>CalculatorForm</tt> code provided above will compile and run, but the [[Qt:Документация 4.3.2/qspinbox | QSpinBox]] objects will not interact with the [[Qt:Документация 4.3.2/qlabel | QLabel]] as we require a custom slot to carry out the add operation and display the result in the [[Qt:Документация 4.3.2/qlabel | QLabel]]. To achieve this, we need to subclass a standard Qt widget.</div></td></tr>
<tr><td class='diff-marker'>-</td><td style="background: #ffa; color:black; font-size: smaller;"><div> </div></td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div><ins class="diffchange diffchange-inline"><div id="thesingleinheritanceapproach"></div><div id="the-single-inheritance-approach"></div></ins></div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>===The Single Inheritance Approach===</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>===The Single Inheritance Approach===</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>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.</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>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.</div></td></tr>
<tr><td colspan="2" class="diff-lineno">Строка 91:</td>
<td colspan="2" class="diff-lineno">Строка 93:</td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>The main advantages of this approach are its simple use of inheritance to provide a [[Qt:Документация 4.3.2/qwidget | QWidget]]-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.</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>The main advantages of this approach are its simple use of inheritance to provide a [[Qt:Документация 4.3.2/qwidget | QWidget]]-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.</div></td></tr>
<tr><td class='diff-marker'>-</td><td style="background: #ffa; color:black; font-size: smaller;"><div> </div></td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div><ins class="diffchange diffchange-inline"><div id="themultipleinheritanceapproach"></div><div id="the-multiple-inheritance-approach"></div></ins></div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>===The Multiple Inheritance Approach===</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>===The Multiple Inheritance Approach===</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>Forms created with ''Qt Designer'' can be subclassed along with a standard [[Qt:Документация 4.3.2/qwidget | QWidget]]-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 [[Qt:Документация 4.3.2/qobject#connect | connect()]] function.</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>Forms created with ''Qt Designer'' can be subclassed along with a standard [[Qt:Документация 4.3.2/qwidget | QWidget]]-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 [[Qt:Документация 4.3.2/qobject#connect | connect()]] function.</div></td></tr>
<tr><td colspan="2" class="diff-lineno">Строка 122:</td>
<td colspan="2" class="diff-lineno">Строка 124:</td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>Subclassing using multiple inheritance gives us more direct access to the contents of the form, is slightly cleaner than the single inheritance approach, but does not conveniently support composition of multiple user interfaces.</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>Subclassing using multiple inheritance gives us more direct access to the contents of the form, is slightly cleaner than the single inheritance approach, but does not conveniently support composition of multiple user interfaces.</div></td></tr>
<tr><td class='diff-marker'>-</td><td style="background: #ffa; color:black; font-size: smaller;"><div> </div></td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div><ins class="diffchange diffchange-inline"><div id="runtimeformprocessing"></div><div id="run-time-form-processing"></div></ins></div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>==Run Time Form Processing==</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>==Run Time Form Processing==</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>Alternatively, forms can be processed at run time, producing dynamically-generated user interfaces. This can be done using the [[Qt:Документация 4.3.2/qtuitools | QtUiTools]] module, which provides the [[Qt:Документация 4.3.2/quiloader | QUiLoader]] class to handle forms created with ''Qt Designer''.</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>Alternatively, forms can be processed at run time, producing dynamically-generated user interfaces. This can be done using the [[Qt:Документация 4.3.2/qtuitools | QtUiTools]] module, which provides the [[Qt:Документация 4.3.2/quiloader | QUiLoader]] class to handle forms created with ''Qt Designer''.</div></td></tr>
<tr><td class='diff-marker'>-</td><td style="background: #ffa; color:black; font-size: smaller;"><div> </div></td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div><ins class="diffchange diffchange-inline"><div id="the-uitools-approach"></div></ins></div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>===The UiTools Approach===</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>===The UiTools Approach===</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>A resource file containing a <tt>.ui</tt> file is required to process forms at run time. Also, the application needs to be configured to use the [[Qt:Документация 4.3.2/qtuitools | QtUiTools]] module. This is done by including the following declaration in a qmake project file, ensuring that the application is compiled and linked appropriately.</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>A resource file containing a <tt>.ui</tt> file is required to process forms at run time. Also, the application needs to be configured to use the [[Qt:Документация 4.3.2/qtuitools | QtUiTools]] module. This is done by including the following declaration in a qmake project file, ensuring that the application is compiled and linked appropriately.</div></td></tr>
<tr><td colspan="2" class="diff-lineno">Строка 151:</td>
<td colspan="2" class="diff-lineno">Строка 153:</td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div> ui_lineEdit = qFindChild<QLineEdit*>(this, "lineEdit");</source> </div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div> ui_lineEdit = qFindChild<QLineEdit*>(this, "lineEdit");</source> </div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>Processing forms at run-time gives the user the freedom to change a program's user interface, just by changing the <tt>.ui</tt> file. This is useful when customizing programs to suit various user needs.</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>Processing forms at run-time gives the user the freedom to change a program's user interface, just by changing the <tt>.ui</tt> file. This is useful when customizing programs to suit various user needs.</div></td></tr>
<tr><td class='diff-marker'>-</td><td style="background: #ffa; color:black; font-size: smaller;"><div> </div></td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div><ins class="diffchange diffchange-inline"><div id="automaticconnections"></div><div id="automatic-connections"></div></ins></div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>==Automatic Connections==</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>==Automatic Connections==</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>The signals and slots connections defined for compile time or run time forms can either be set up manually or automatically, using [[Qt:Документация 4.3.2/qmetaobject | QMetaObject]]'s ability to make connections between signals and suitably-named slots.</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>The signals and slots connections defined for compile time or run time forms can either be set up manually or automatically, using [[Qt:Документация 4.3.2/qmetaobject | QMetaObject]]'s ability to make connections between signals and suitably-named slots.</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>Generally, in a [[Qt:Документация 4.3.2/qdialog | QDialog]], if we want to process the information entered by the user before accepting it, we need to connect the clicked() signal from the '''OK''' 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.</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>Generally, in a [[Qt:Документация 4.3.2/qdialog | QDialog]], if we want to process the information entered by the user before accepting it, we need to connect the clicked() signal from the '''OK''' 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.</div></td></tr>
<tr><td class='diff-marker'>-</td><td style="background: #ffa; color:black; font-size: smaller;"><div> </div></td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div><ins class="diffchange diffchange-inline"><div id="adialogwithoutautoconnect"></div><div id="a-dialog-without-auto-connect"></div></ins></div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>===A Dialog Without Auto-Connect===</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>===A Dialog Without Auto-Connect===</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>We define the dialog in the same way as before, but now include a slot in addition to the constructor:</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>We define the dialog in the same way as before, but now include a slot in addition to the constructor:</div></td></tr>
<tr><td colspan="2" class="diff-lineno">Строка 191:</td>
<td colspan="2" class="diff-lineno">Строка 193:</td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div> }</source> </div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div> }</source> </div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>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.</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>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.</div></td></tr>
<tr><td class='diff-marker'>-</td><td style="background: #ffa; color:black; font-size: smaller;"><div> </div></td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div><ins class="diffchange diffchange-inline"><div id="widgetsanddialogswithautoconnect"></div><div id="widgets-and-dialogs-with-auto-connect"></div></ins></div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>===Widgets and Dialogs with Auto-Connect===</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>===Widgets and Dialogs with Auto-Connect===</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>Although it is easy to implement a custom slot in the dialog and connect it in the constructor, we could instead use [[Qt:Документация 4.3.2/qmetaobject | QMetaObject]]'s auto-connection facilities to connect the '''OK''' 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:</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>Although it is easy to implement a custom slot in the dialog and connect it in the constructor, we could instead use [[Qt:Документация 4.3.2/qmetaobject | QMetaObject]]'s auto-connection facilities to connect the '''OK''' 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:</div></td></tr>
<!-- diff generator: internal 2024-03-29 13:27:47 -->
</table>Roothttp://wiki.crossplatform.ru/index.php?title=Qt:%D0%94%D0%BE%D0%BA%D1%83%D0%BC%D0%B5%D0%BD%D1%82%D0%B0%D1%86%D0%B8%D1%8F_4.3.2/designer-using-a-component&diff=1822&oldid=prevRoot в 13:20, 30 октября 20082008-10-30T13:20:46Z<p></p>
<table style="background-color: white; color:black;">
<col class='diff-marker' />
<col class='diff-content' />
<col class='diff-marker' />
<col class='diff-content' />
<tr valign='top'>
<td colspan='2' style="background-color: white; color:black;">← Предыдущая</td>
<td colspan='2' style="background-color: white; color:black;">Версия 13:20, 30 октября 2008</td>
</tr>
<tr><td colspan="2" class="diff-lineno">Строка 4:</td>
<td colspan="2" class="diff-lineno">Строка 4:</td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>=Using a Component in Your Application<br />=</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>=Using a Component in Your Application<br />=</div></td></tr>
<tr><td class='diff-marker'>-</td><td style="background: #ffa; color:black; font-size: smaller;"><div>*[[#<del class="diffchange diffchange-inline">compile</del>-<del class="diffchange diffchange-inline">time</del>-<del class="diffchange diffchange-inline">form</del>-<del class="diffchange diffchange-inline">processing </del>| Compile Time Form Processing]]</div></td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div>*[[#<ins class="diffchange diffchange-inline">Compile</ins>-<ins class="diffchange diffchange-inline">Time</ins>-<ins class="diffchange diffchange-inline">Form</ins>-<ins class="diffchange diffchange-inline">Processing </ins>| Compile Time Form Processing]]</div></td></tr>
<tr><td class='diff-marker'>-</td><td style="background: #ffa; color:black; font-size: smaller;"><div>**[[#<del class="diffchange diffchange-inline">the</del>-<del class="diffchange diffchange-inline">direct</del>-<del class="diffchange diffchange-inline">approach </del>| The Direct Approach]]</div></td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div>**[[#<ins class="diffchange diffchange-inline">The</ins>-<ins class="diffchange diffchange-inline">Direct</ins>-<ins class="diffchange diffchange-inline">Approach </ins>| The Direct Approach]]</div></td></tr>
<tr><td class='diff-marker'>-</td><td style="background: #ffa; color:black; font-size: smaller;"><div>**[[#<del class="diffchange diffchange-inline">the</del>-<del class="diffchange diffchange-inline">single</del>-<del class="diffchange diffchange-inline">inheritance</del>-<del class="diffchange diffchange-inline">approach </del>| The Single Inheritance Approach]]</div></td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div>**[[#<ins class="diffchange diffchange-inline">The</ins>-<ins class="diffchange diffchange-inline">Single</ins>-<ins class="diffchange diffchange-inline">Inheritance</ins>-<ins class="diffchange diffchange-inline">Approach </ins>| The Single Inheritance Approach]]</div></td></tr>
<tr><td class='diff-marker'>-</td><td style="background: #ffa; color:black; font-size: smaller;"><div>**[[#<del class="diffchange diffchange-inline">the</del>-<del class="diffchange diffchange-inline">multiple</del>-<del class="diffchange diffchange-inline">inheritance</del>-<del class="diffchange diffchange-inline">approach </del>| The Multiple Inheritance Approach]]</div></td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div>**[[#<ins class="diffchange diffchange-inline">The</ins>-<ins class="diffchange diffchange-inline">Multiple</ins>-<ins class="diffchange diffchange-inline">Inheritance</ins>-<ins class="diffchange diffchange-inline">Approach </ins>| The Multiple Inheritance Approach]]</div></td></tr>
<tr><td class='diff-marker'>-</td><td style="background: #ffa; color:black; font-size: smaller;"><div>*[[#<del class="diffchange diffchange-inline">run</del>-<del class="diffchange diffchange-inline">time</del>-<del class="diffchange diffchange-inline">form</del>-<del class="diffchange diffchange-inline">processing </del>| Run Time Form Processing]]</div></td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div>*[[#<ins class="diffchange diffchange-inline">Run</ins>-<ins class="diffchange diffchange-inline">Time</ins>-<ins class="diffchange diffchange-inline">Form</ins>-<ins class="diffchange diffchange-inline">Processing </ins>| Run Time Form Processing]]</div></td></tr>
<tr><td class='diff-marker'>-</td><td style="background: #ffa; color:black; font-size: smaller;"><div>**[[#<del class="diffchange diffchange-inline">the</del>-<del class="diffchange diffchange-inline">uitools</del>-<del class="diffchange diffchange-inline">approach </del>| The UiTools Approach]]</div></td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div>**[[#<ins class="diffchange diffchange-inline">The</ins>-<ins class="diffchange diffchange-inline">Uitools</ins>-<ins class="diffchange diffchange-inline">Approach </ins>| The UiTools Approach]]</div></td></tr>
<tr><td class='diff-marker'>-</td><td style="background: #ffa; color:black; font-size: smaller;"><div>*[[#<del class="diffchange diffchange-inline">automatic</del>-<del class="diffchange diffchange-inline">connections </del>| Automatic Connections]]</div></td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div>*[[#<ins class="diffchange diffchange-inline">Automatic</ins>-<ins class="diffchange diffchange-inline">Connections </ins>| Automatic Connections]]</div></td></tr>
<tr><td class='diff-marker'>-</td><td style="background: #ffa; color:black; font-size: smaller;"><div>**[[#<del class="diffchange diffchange-inline">a</del>-<del class="diffchange diffchange-inline">dialog</del>-<del class="diffchange diffchange-inline">without</del>-<del class="diffchange diffchange-inline">auto</del>-<del class="diffchange diffchange-inline">connect </del>| A Dialog Without Auto-Connect]]</div></td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div>**[[#<ins class="diffchange diffchange-inline">A</ins>-<ins class="diffchange diffchange-inline">Dialog</ins>-<ins class="diffchange diffchange-inline">Without</ins>-<ins class="diffchange diffchange-inline">Auto</ins>-<ins class="diffchange diffchange-inline">Connect </ins>| A Dialog Without Auto-Connect]]</div></td></tr>
<tr><td class='diff-marker'>-</td><td style="background: #ffa; color:black; font-size: smaller;"><div>**[[#<del class="diffchange diffchange-inline">widgets</del>-and-<del class="diffchange diffchange-inline">dialogs</del>-with-<del class="diffchange diffchange-inline">auto</del>-<del class="diffchange diffchange-inline">connect </del>| Widgets and Dialogs with Auto-Connect]]</div></td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div>**[[#<ins class="diffchange diffchange-inline">Widgets</ins>-and-<ins class="diffchange diffchange-inline">Dialogs</ins>-with-<ins class="diffchange diffchange-inline">Auto</ins>-<ins class="diffchange diffchange-inline">Connect </ins>| Widgets and Dialogs with Auto-Connect]]</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"></td></tr>
<!-- diff generator: internal 2024-03-29 13:27:47 -->
</table>Roothttp://wiki.crossplatform.ru/index.php?title=Qt:%D0%94%D0%BE%D0%BA%D1%83%D0%BC%D0%B5%D0%BD%D1%82%D0%B0%D1%86%D0%B8%D1%8F_4.3.2/designer-using-a-component&diff=1821&oldid=prevRoot: Новая: {{Qt4.3.2_header}} [Previous: Using Custom Widgets with Qt Designer ] [ [[Qt:Документация 4.3.2/design...2008-10-30T13:18:54Z<p>Новая: {{Qt4.3.2_header}} [Previous: <a href="/index.php/Qt:%D0%94%D0%BE%D0%BA%D1%83%D0%BC%D0%B5%D0%BD%D1%82%D0%B0%D1%86%D0%B8%D1%8F_4.3.2/designer-using-custom-widgets" title="Qt:Документация 4.3.2/designer-using-custom-widgets"> Using Custom Widgets with Qt Designer</a> ] [ [[Qt:Документация 4.3.2/design...</p>
<p><b>Новая страница</b></p><div>{{Qt4.3.2_header}}<br />
<br />
[Previous: [[Qt:Документация 4.3.2/designer-using-custom-widgets | Using Custom Widgets with Qt Designer]] ] [ [[Qt:Документация 4.3.2/designer-manual | Contents]] ] [Next: [[Qt:Документация 4.3.2/designer-creating-custom-widgets | Creating Custom Widgets for Qt Designer]] ] <br />
<br />
=Using a Component in Your Application<br />=<br />
*[[#compile-time-form-processing | Compile Time Form Processing]]<br />
**[[#the-direct-approach | The Direct Approach]]<br />
**[[#the-single-inheritance-approach | The Single Inheritance Approach]]<br />
**[[#the-multiple-inheritance-approach | The Multiple Inheritance Approach]]<br />
*[[#run-time-form-processing | Run Time Form Processing]]<br />
**[[#the-uitools-approach | The UiTools Approach]]<br />
*[[#automatic-connections | Automatic Connections]]<br />
**[[#a-dialog-without-auto-connect | A Dialog Without Auto-Connect]]<br />
**[[#widgets-and-dialogs-with-auto-connect | Widgets and Dialogs with Auto-Connect]]<br />
<br />
<br />
With Qt's integrated build tools, [[Qt:Документация 4.3.2/qmake-manual | qmake]] and [[Qt:Документация 4.3.2/uic#uic | uic]], the code for user interface components created with ''Qt Designer'' is automatically generated when the rest of of your application is built. Forms can be included and used directly from your application, or you can use them to extend subclasses of standard widgets. These forms can be processed at compile time or at run time, depending on the approach used.<br />
<br />
==Compile Time Form Processing==<br />
A compile time processed form can be used in your application with any of the following approaches:<br />
<br />
*In the direct approach, you simply construct a widget to use as a placeholder for the component, and set up the user interface inside it.<br />
*In the single inheritance approach, you subclass the form's base class ([[Qt:Документация 4.3.2/qwidget | QWidget]] or [[Qt:Документация 4.3.2/qdialog | QDialog]], for example), and include a private instance of the form's user interface object.<br />
*In the multiple inheritance approach, you subclass from 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.<br />
<br />
===The Direct Approach===<br />
To demonstrate how user interface components can be used straight from ''Qt Designer'', we create a simple Calculator Form application, based on the original [[Qt:Документация 4.3.2/designer-calculatorform | Calculator Form]] example.<br />
<br />
The application consists of one source file, <tt>main.cpp</tt>, and a <tt>.ui</tt> file.<br />
<br />
The <tt>calculatorform.ui</tt> file designed with ''Qt Designer'' is shown below:<br />
<br />
[[Image:directapproach-calculatorform.png|center]]<br />
<br />
We will use <tt>qmake</tt> to build the executable, so we need to write a <tt>.pro</tt> file:<br />
<source lang="cpp-qt"> TEMPLATE = app<br />
FORMS = calculatorform.ui<br />
SOURCES = main.cpp</source> <br />
The special feature of this file is the <tt>FORMS</tt> declaration that tells <tt>qmake</tt> which files it needs 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 files listed in the <tt>SOURCES</tt> declaration. To ensure that <tt>qmake</tt> generates the <tt>ui_calculatorform.h</tt> file, we need to include it in a file listed in <tt>SOURCES</tt>. Since we only have <tt>main.cpp</tt>, we include it there:<br />
<source lang="cpp-qt"> #include "ui_calculatorform.h"</source> <br />
This additional check ensures that we do not generate code for ''.ui'' files that are not used.<br />
<br />
The <tt>main</tt> function creates the calculator widget by constructing a standard [[Qt:Документация 4.3.2/qwidget | QWidget]] that we use to host the user interface described by the <tt>calculatorform.ui</tt> file.<br />
<source lang="cpp-qt"> int main(int argc, char *argv[])<br />
{<br />
QApplication app(argc, argv);<br />
QWidget *widget = new QWidget;<br />
Ui::CalculatorForm ui;<br />
ui.setupUi(widget);<br />
<br />
widget->show();<br />
return app.exec();<br />
}</source> <br />
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.<br />
<br />
This approach provides a quick and easy way to use simple, self-contained components in your applications, but many components created with ''Qt Designer'' will need to be integrated more closely with the rest of the application code. For instance, the <tt>CalculatorForm</tt> code provided above will compile and run, but the [[Qt:Документация 4.3.2/qspinbox | QSpinBox]] objects will not interact with the [[Qt:Документация 4.3.2/qlabel | QLabel]] as we require a custom slot to carry out the add operation and display the result in the [[Qt:Документация 4.3.2/qlabel | QLabel]]. To achieve this, we need to subclass a standard Qt widget.<br />
<br />
===The Single Inheritance Approach===<br />
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.<br />
<br />
This approach is used in the [[Qt:Документация 4.3.2/designer-calculatorform | Calculator Form]] example.<br />
<br />
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>:<br />
<source lang="cpp-qt"> #include "ui_calculatorform.h"</source> <br />
This means that the <tt>.pro</tt> file must be updated to include <tt>calculatorform.h</tt>:<br />
<source lang="cpp-qt"> HEADERS = calculatorform.h</source> <br />
The subclass is defined in the following way:<br />
<source lang="cpp-qt"> class CalculatorForm : public QWidget<br />
{<br />
Q_OBJECT<br />
<br />
public:<br />
CalculatorForm(QWidget *parent = 0);<br />
<br />
private slots:<br />
void on_inputSpinBox1_valueChanged(int value);<br />
void on_inputSpinBox2_valueChanged(int value);<br />
<br />
private:<br />
Ui::CalculatorForm ui;<br />
};</source> <br />
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.<br />
<br />
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.<br />
<source lang="cpp-qt"> CalculatorForm::CalculatorForm(QWidget *parent)<br />
: QWidget(parent)<br />
{<br />
ui.setupUi(this);<br />
}</source> <br />
We can connect signals from the user interface widgets to slots in the widget in the usual way, taking care to prefix the <tt>ui</tt> object to each widget used.<br />
<br />
The main advantages of this approach are its simple use of inheritance to provide a [[Qt:Документация 4.3.2/qwidget | QWidget]]-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.<br />
<br />
===The Multiple Inheritance Approach===<br />
Forms created with ''Qt Designer'' can be subclassed along with a standard [[Qt:Документация 4.3.2/qwidget | QWidget]]-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 [[Qt:Документация 4.3.2/qobject#connect | connect()]] function.<br />
<br />
This approach is used in the [[Qt:Документация 4.3.2/uitools-multipleinheritance | Multiple Inheritance]] example.<br />
<br />
We need to include the header file that <tt>uic</tt> generates from the <tt>calculatorform.ui</tt> file:<br />
<source lang="cpp-qt"> #include "ui_calculatorform.h"</source> <br />
The class is defined in a similar way to the one used in the private interface approach, except that this time we inherit from both [[Qt:Документация 4.3.2/qwidget | QWidget]] and <tt>Ui::CalculatorForm</tt>:<br />
<source lang="cpp-qt"> class CalculatorForm : public QWidget, private Ui::CalculatorForm<br />
{<br />
Q_OBJECT<br />
<br />
public:<br />
CalculatorForm(QWidget *parent = 0);<br />
<br />
private slots:<br />
void on_inputSpinBox1_valueChanged(int value);<br />
void on_inputSpinBox2_valueChanged(int value);<br />
};</source> <br />
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.<br />
<br />
The constructor for the subclass performs many of the same tasks as the constructor used in the private interface example:<br />
<source lang="cpp-qt"> CalculatorForm::CalculatorForm(QWidget *parent)<br />
: QWidget(parent)<br />
{<br />
setupUi(this);<br />
}</source> <br />
In this case, the widgets used in the user interface can be accessed in the same way as a widget created in code by hand. We no longer require the <tt>ui</tt> prefix to access them.<br />
<br />
Subclassing using multiple inheritance gives us more direct access to the contents of the form, is slightly cleaner than the single inheritance approach, but does not conveniently support composition of multiple user interfaces.<br />
<br />
==Run Time Form Processing==<br />
Alternatively, forms can be processed at run time, producing dynamically-generated user interfaces. This can be done using the [[Qt:Документация 4.3.2/qtuitools | QtUiTools]] module, which provides the [[Qt:Документация 4.3.2/quiloader | QUiLoader]] class to handle forms created with ''Qt Designer''.<br />
<br />
===The UiTools Approach===<br />
A resource file containing a <tt>.ui</tt> file is required to process forms at run time. Also, the application needs to be configured to use the [[Qt:Документация 4.3.2/qtuitools | QtUiTools]] module. This is done by including the following declaration in a qmake project file, ensuring that the application is compiled and linked appropriately.<br />
<source lang="cpp-qt"> CONFIG += uitools</source> <br />
The [[Qt:Документация 4.3.2/quiloader | QUiLoader]] class provides a form loader object to construct the user interface. This user interface can be retrieved from any [[Qt:Документация 4.3.2/qiodevice | QIODevice]] (a [[Qt:Документация 4.3.2/qfile | QFile]] object for example) to obtain a form stored in a project's resources. The [[Qt:Документация 4.3.2/quiloader#load | QUiLoader::load]]() function constructs the form widget using the user interface description contained in the file.<br />
<br />
The [[Qt:Документация 4.3.2/qtuitools | QtUiTools]] module classes can be included using the following directive:<br />
<source lang="cpp-qt"> #include <QtUiTools></source> <br />
The [[Qt:Документация 4.3.2/quiloader#load | QUiLoader::load]]() function is invoked as shown in this code from the [[Qt:Документация 4.3.2/uitools-textfinder | Text Finder]] example:<br />
<source lang="cpp-qt"> QWidget* TextFinder::loadUiFile()<br />
{<br />
QUiLoader loader;<br />
<br />
QFile file(":/forms/textfinder.ui");<br />
file.open(QFile::ReadOnly);<br />
<br />
QWidget *formWidget = loader.load(&amp;file, this);<br />
file.close();<br />
<br />
return formWidget;<br />
}</source> <br />
In a class that uses [[Qt:Документация 4.3.2/qtuitools | QtUiTools]] to build its user interface at run time, we can locate objects in the form using [[Qt:Документация 4.3.2/qobject#qFindChild | qFindChild]](). For example, in the following code, we locate some components based on their object names and widget types:<br />
<source lang="cpp-qt"> ui_findButton = qFindChild<QPushButton*>(this, "findButton");<br />
ui_textEdit = qFindChild<QTextEdit*>(this, "textEdit");<br />
ui_lineEdit = qFindChild<QLineEdit*>(this, "lineEdit");</source> <br />
Processing forms at run-time gives the user the freedom to change a program's user interface, just by changing the <tt>.ui</tt> file. This is useful when customizing programs to suit various user needs.<br />
<br />
==Automatic Connections==<br />
The signals and slots connections defined for compile time or run time forms can either be set up manually or automatically, using [[Qt:Документация 4.3.2/qmetaobject | QMetaObject]]'s ability to make connections between signals and suitably-named slots.<br />
<br />
Generally, in a [[Qt:Документация 4.3.2/qdialog | QDialog]], if we want to process the information entered by the user before accepting it, we need to connect the clicked() signal from the '''OK''' 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.<br />
<br />
===A Dialog Without Auto-Connect===<br />
We define the dialog in the same way as before, but now include a slot in addition to the constructor:<br />
<source lang="cpp-qt"> class ImageDialog : public QDialog, private Ui::ImageDialog<br />
{<br />
Q_OBJECT<br />
<br />
public:<br />
ImageDialog(QWidget *parent = 0);<br />
<br />
private slots:<br />
void checkValues();<br />
};</source> <br />
The <tt>checkValues()</tt> slot will be used to validate the values provided by the user.<br />
<br />
In the dialog's constructor we set up the widgets as before, and connect the '''Cancel''' button's [[Qt:Документация 4.3.2/qabstractbutton#clicked | clicked()]] signal to the dialog's reject() slot. We also disable the [[Qt:Документация 4.3.2/qpushbutton#autoDefault-prop | autoDefault]] property in both buttons to ensure that the dialog does not interfere with the way that the line edit handles return key events:<br />
<source lang="cpp-qt"> ImageDialog::ImageDialog(QWidget *parent)<br />
: QDialog(parent)<br />
{<br />
setupUi(this);<br />
okButton->setAutoDefault(false);<br />
cancelButton->setAutoDefault(false);<br />
...<br />
connect(okButton, SIGNAL(clicked()), this, SLOT(checkValues()));<br />
}</source> <br />
We connect the '''OK''' button's [[Qt:Документация 4.3.2/qabstractbutton#clicked | clicked()]] signal to the dialog's checkValues() slot which we implement as follows:<br />
<source lang="cpp-qt"> void ImageDialog::checkValues()<br />
{<br />
if (nameLineEdit->text().isEmpty())<br />
(void) QMessageBox::information(this, tr("No Image Name"),<br />
tr("Please supply a name for the image."), QMessageBox::Cancel);<br />
else<br />
accept();<br />
}</source> <br />
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.<br />
<br />
===Widgets and Dialogs with Auto-Connect===<br />
Although it is easy to implement a custom slot in the dialog and connect it in the constructor, we could instead use [[Qt:Документация 4.3.2/qmetaobject | QMetaObject]]'s auto-connection facilities to connect the '''OK''' 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:<br />
<source lang="cpp-qt"> void on_<widget name>_<signal name>(<signal parameters>);</source> <br />
Using this convention, we can define and implement a slot that responds to mouse clicks on the '''OK''' button:<br />
<source lang="cpp-qt"> class ImageDialog : public QDialog, private Ui::ImageDialog<br />
{<br />
Q_OBJECT<br />
<br />
public:<br />
ImageDialog(QWidget *parent = 0);<br />
<br />
private slots:<br />
void on_okButton_clicked();<br />
};</source> <br />
Another example of automatic signal and slot connection would be the [[Qt:Документация 4.3.2/uitools-textfinder | Text Finder]] with its <tt>on_findButton_clicked()</tt> slot.<br />
<br />
We use [[Qt:Документация 4.3.2/qmetaobject | QMetaObject]]'s system to enable signal and slot connections:<br />
<source lang="cpp-qt"> QMetaObject::connectSlotsByName(this);</source> <br />
This enables us to implement the slot, as shown below:<br />
<source lang="cpp-qt"> void TextFinder::on_findButton_clicked()<br />
{<br />
QString searchString = ui_lineEdit->text();<br />
QTextDocument *document = ui_textEdit->document();<br />
<br />
bool found = false;<br />
<br />
if (isFirstTime == false)<br />
document->undo();<br />
<br />
if (searchString == "") {<br />
QMessageBox::information(this, tr("Empty Search Field"),<br />
"The search field is empty. Please enter a word and click Find.");<br />
} else {<br />
<br />
QTextCursor highlightCursor(document);<br />
QTextCursor cursor(document);<br />
<br />
cursor.beginEditBlock();<br />
...<br />
cursor.endEditBlock();<br />
isFirstTime = false;<br />
<br />
if (found == false) {<br />
QMessageBox::information(this, tr("Word Not Found"),<br />
"Sorry, the word cannot be found.");<br />
}<br />
}<br />
}</source> <br />
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.<br />
<br />
[Previous: [[Qt:Документация 4.3.2/designer-using-custom-widgets | Using Custom Widgets with Qt Designer]] ] [ [[Qt:Документация 4.3.2/designer-manual | Contents]] ] [Next: [[Qt:Документация 4.3.2/designer-creating-custom-widgets | Creating Custom Widgets for Qt Designer]] ] <br />
<br />
{{Qt4.3.2_footer}}</div>Root