Sophie

Sophie

distrib > CentOS > 5 > x86_64 > by-pkgid > ac91357d6caede925de099a02fced14e > files > 2069

qt4-doc-4.2.1-1.el5_7.1.x86_64.rpm

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<!-- /tmp/qt-4.2.1-harald-1161357942206/qt-x11-opensource-src-4.2.1/doc/src/designer-manual.qdoc -->
<head>
  <title>Qt 4.2: Using a Component in Your Application</title>
  <link rel="prev" href="designer-using-custom-widgets.html" />
  <link rel="contents" href="designer-manual.html" />
  <link rel="next" href="designer-creating-custom-widgets.html" />
  <link href="classic.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="left" valign="top" width="32"><a href="http://www.trolltech.com/products/qt"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></a></td>
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a>&nbsp;&middot; <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a>&nbsp;&middot; <a href="modules.html"><font color="#004faf">Modules</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">Functions</font></a></td>
<td align="right" valign="top" width="230"><a href="http://www.trolltech.com"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></a></td></tr></table><p>
[Previous: <a href="designer-using-custom-widgets.html">Using Custom Widgets with Qt Designer</a>]
[<a href="designer-manual.html">Contents</a>]
[Next: <a href="designer-creating-custom-widgets.html">Creating Custom Widgets for Qt Designer</a>]
</p>
<h1 align="center">Using a Component in Your Application<br /><small></small></h1>
<ul><li><a href="#the-direct-approach">The Direct Approach</a></li>
<li><a href="#the-single-inheritance-approach">The Single Inheritance Approach</a></li>
<li><a href="#the-multiple-inheritance-approach">The Multiple Inheritance Approach</a></li>
<li><a href="#automatic-connections">Automatic Connections</a></li>
<ul><li><a href="#a-dialog-without-auto-connect">A Dialog Without Auto-Connect</a></li>
<li><a href="#a-dialog-with-auto-connect">A Dialog With Auto-Connect</a></li>
</ul>
</ul>
<p>With Qt's integrated build tools, <a href="qmake-manual.html">qmake</a> and <a href="uic.html#uic">uic</a>, the code for user interface components created with <i>Qt Designer</i> 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.</p>
<p>There are three approaches to using a form in your application:</p>
<ul>
<li>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.</li>
<li>In the single inheritance approach, you subclass the form's base class (<a href="qwidget.html">QWidget</a> or <a href="qdialog.html">QDialog</a>, for example), and include a private instance of the form's user interface object.</li>
<li>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.</li>
</ul>
<p>Additionally, the signals and slots connections defined for the form can be set up manually, or you can take advantage of <a href="qmetaobject.html">QMetaObject</a>'s ability to make connections between signals and suitably-named slots.</p>
<a name="thedirectapproach"></a><a name="the-direct-approach"></a>
<h2>The Direct Approach</h2>
<p>To demonstrate how user interface components can be used straight from <i>Qt Designer</i>, we take the dialog that we created in the chapter on <a href="designer-designing-a-component.html">Designing a Component</a>, and we create a simple application to display it.</p>
<p>The source of the application consists of two files: <tt>main.cpp</tt> and <tt>imagedialog.ui</tt>. We will use <tt>qmake</tt> to build the executable, so we need to write a <tt>.pro</tt> file:</p>
<pre> TEMPLATE    = app
 FORMS       = imagedialog.ui
 SOURCES     = main.cpp</pre>
<p>The special feature of this file is the <tt>FORMS</tt> declaration that tells <tt>qmake</tt> which files it needs to process with <tt>uic</tt>. In this case, the <tt>imagedialog.ui</tt> file is used to create a <tt>ui_imagedialog.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_imagedialog.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:</p>
<pre> #include &quot;ui_imagedialog.h&quot;
 #include &lt;QApplication&gt;</pre>
<p>This additional check ensures that we do not generate code for <i>.ui</i> files that are not used.</p>
<p>The <tt>main</tt> function creates the image dialog by constructing a standard <a href="qdialog.html">QDialog</a> that we use to host the user interface described by the <tt>imagedialog.ui</tt> file.</p>
<pre> int main(int argc, char *argv[])
 {
     QApplication app(argc, argv);
     QDialog *window = new QDialog;
     Ui::ImageDialog ui;
     ui.setupUi(window);

     window-&gt;show();
     return app.exec();
 }</pre>
<p>In this case, the <tt>Ui::ImageDialog</tt> is an interface description object from the <tt>ui_imagedialog.h</tt> file that sets up all the dialog's widgets and the connections between its signals and slots.</p>
<p>This approach provides a quick and easy way to use simple, self-contained components in your applications, but many components created with <i>Qt Designer</i> will need to be integrated more closely with the rest of the application code. To achieve this, we need to subclass a standard Qt widget.</p>
<a name="thesingleinheritanceapproach"></a><a name="the-single-inheritance-approach"></a>
<h2>The Single Inheritance Approach</h2>
<p>In this approach, we use the <tt>Ui::ImageDialog</tt> object as we did in the simple case, but instead of setting up the dialog from the application's main function, we subclass <a href="qdialog.html">QDialog</a> 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 <a href="qdialog.html">QDialog</a> subclass, and provide a standard system for making signal and slot connections between the user interface and other objects in your application.</p>
<p>This approach is used in the <a href="designer-calculatorform.html">Calculator Form</a> example.</p>
<p>To ensure that we can use the user interface, we need to include the header file that <tt>uic</tt> generates before referring to <tt>Ui::ImageDialog</tt>:</p>
<pre> #include &quot;ui_imagedialog.h&quot;</pre>
<p>The subclass is defined in the following way:</p>
<pre> class ImageDialog : public QDialog
 {
     Q_OBJECT

 public:
     ImageDialog(QWidget *parent = 0);

 private:
     Ui::ImageDialog ui;
 };</pre>
<p>The important features of the class are the private <tt>ui</tt> object which provides the code for setting up and managing the user interface.</p>
<p>The constructor for the subclass constructs and configures all the widgets and layouts for the dialog just by calling the <tt>ui</tt> object's setupUi() function. Once this has been done, it is possible to modify the user interface and create items for the combobox:</p>
<pre> ImageDialog::ImageDialog(QWidget *parent)
     : QDialog(parent)
 {
     ui.setupUi(this);

     ui.colorDepthCombo-&gt;addItem(tr(&quot;2 colors (1 bit per pixel)&quot;));
     ui.colorDepthCombo-&gt;addItem(tr(&quot;4 colors (2 bits per pixel)&quot;));
     ui.colorDepthCombo-&gt;addItem(tr(&quot;16 colors (4 bits per pixel)&quot;));
     ui.colorDepthCombo-&gt;addItem(tr(&quot;256 colors (8 bits per pixel)&quot;));
     ui.colorDepthCombo-&gt;addItem(tr(&quot;65536 colors (16 bits per pixel)&quot;));
     ui.colorDepthCombo-&gt;addItem(tr(&quot;16 million colors (24 bits per pixel)&quot;));

     connect(ui.okButton, SIGNAL(clicked()), this, SLOT(accept()));
     connect(ui.cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
 }</pre>
<p>We can connect signals from the user interface widgets to slots in the dialog in the usual way, taking care to prefix the <tt>ui</tt> object to each widget used.</p>
<p>The main advantages of this approach are its simple use of inheritance to provide a <a href="qdialog.html">QDialog</a>-based interface, and its encapsulation of the user interface widget variables within the <tt>ui</tt> data member. We can use this method to define a number of user interfaces within the same widget, each of which is contained within its own namespace, and overlay (or &quot;compose&quot;) them. This approach can be used to create individual tabs from existing forms, for example.</p>
<a name="themultipleinheritanceapproach"></a><a name="the-multiple-inheritance-approach"></a>
<h2>The Multiple Inheritance Approach</h2>
<p>Forms created with <i>Qt Designer</i> can be subclassed along with a standard <a href="qwidget.html">QWidget</a>-based class. This approach makes all the user interface components defined in the form directly accessible within the scope of the subclass, and enables signal and slot connections to be made in the usual way with the <a href="qobject.html#connect">connect()</a> function.</p>
<p>As before, we need to include the header file that <tt>uic</tt> generates from the <tt>imagedialog.ui</tt> file:</p>
<pre> #include &quot;ui_imagedialog.h&quot;</pre>
<p>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 <a href="qdialog.html">QDialog</a> and <tt>Ui::ImageDialog</tt>:</p>
<pre> class ImageDialog : public QDialog, private Ui::ImageDialog
 {
     Q_OBJECT

 public:
     ImageDialog(QWidget *parent = 0);
 };</pre>
<p>We inherit <tt>Ui::ImageDialog</tt> privately to ensure that the user interface objects are private in our subclass. We can also inherit it with the <tt>public</tt> or <tt>protected</tt> keywords in the same way that we could have made <tt>ui</tt> public or protected in the previous case.</p>
<p>The constructor for the subclass performs many of the same tasks as the constructor used in the private interface example:</p>
<pre> ImageDialog::ImageDialog(QWidget *parent)
     : QDialog(parent)
 {
     setupUi(this);

     colorDepthCombo-&gt;addItem(tr(&quot;2 colors (1 bit per pixel)&quot;));
     colorDepthCombo-&gt;addItem(tr(&quot;4 colors (2 bits per pixel)&quot;));
     colorDepthCombo-&gt;addItem(tr(&quot;16 colors (4 bits per pixel)&quot;));
     colorDepthCombo-&gt;addItem(tr(&quot;256 colors (8 bits per pixel)&quot;));
     colorDepthCombo-&gt;addItem(tr(&quot;65536 colors (16 bits per pixel)&quot;));
     colorDepthCombo-&gt;addItem(tr(&quot;16 million colors (24 bits per pixel)&quot;));

     connect(okButton, SIGNAL(clicked()), this, SLOT(accept()));
     connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
 }</pre>
<p>In this case, the interface can be set up using a member function, and the combobox is accessed in the same way as a widget created in code by hand. The push buttons are also referred to directly in the connect() function calls.</p>
<p>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.</p>
<a name="automaticconnections"></a><a name="automatic-connections"></a>
<h2>Automatic Connections</h2>
<p>In the previous sections, we connected the <b>OK</b> and <b>Cancel</b> buttons in the form directly to the dialog's accept() and reject() slots because we did not need to perform any processing on the contents of the dialog. 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="adialogwithoutautoconnect"></a><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 ImageDialog : public QDialog, private Ui::ImageDialog
 {
     Q_OBJECT

 public:
     ImageDialog(QWidget *parent = 0);

 private slots:
     void checkValues();
 };</pre>
<p>The <tt>checkValues()</tt> slot will be used to validate the values provided by the user.</p>
<p>In the dialog's constructor we set up the widgets as before, and connect the <b>Cancel</b> button's clicked() signal to the dialog's reject() slot. We also disable the <a href="qpushbutton.html#autoDefault-prop">autoDefault</a> property in both buttons to ensure that the dialog does not interfere with the way that the line edit handles return key events:</p>
<pre> ImageDialog::ImageDialog(QWidget *parent)
     : QDialog(parent)
 {
     setupUi(this);
     okButton-&gt;setAutoDefault(false);
     cancelButton-&gt;setAutoDefault(false);
     ...
     connect(okButton, SIGNAL(clicked()), this, SLOT(checkValues()));
 }</pre>
<p>We connect the <b>OK</b> button's clicked() signal to the dialog's checkValues() slot which we implement as follows:</p>
<pre> void ImageDialog::checkValues()
 {
     if (nameLineEdit-&gt;text().isEmpty())
         (void) QMessageBox::information(this, tr(&quot;No Image Name&quot;),
             tr(&quot;Please supply a name for the image.&quot;), QMessageBox::Cancel);
     else
         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="adialogwithautoconnect"></a><a name="a-dialog-with-auto-connect"></a>
<h3>A Dialog With Auto-Connect</h3>
<p>Although it is easy to implement a custom slot in the dialog and connect it in the constructor, we could instead use <a href="qmetaobject.html">QMetaObject</a>'s auto-connection facilities to connect the <b>OK</b> button's clicked() signal to a slot in our subclass. <tt>uic</tt> automatically generates code in the dialog's setupUi() function to do this, so we only need to declare and implement a slot with a name that follows a standard convention:</p>
<pre> void on_&lt;widget name&gt;_&lt;signal name&gt;(&lt;signal parameters&gt;);</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 ImageDialog : public QDialog, private Ui::ImageDialog
 {
     Q_OBJECT

 public:
     ImageDialog(QWidget *parent = 0);

 private slots:
     void on_okButton_clicked();
 };</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>
<p>
[Previous: <a href="designer-using-custom-widgets.html">Using Custom Widgets with Qt Designer</a>]
[<a href="designer-manual.html">Contents</a>]
[Next: <a href="designer-creating-custom-widgets.html">Creating Custom Widgets for Qt Designer</a>]
</p>
<p /><address><hr /><div align="center">
<table width="100%" cellspacing="0" border="0"><tr class="address">
<td width="30%">Copyright &copy; 2006 <a href="trolltech.html">Trolltech</a></td>
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
<td width="30%" align="right"><div align="right">Qt 4.2.1</div></td>
</tr></table></div></address></body>
</html>