~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to doc/html/designer-calculatorform.html

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?xml version="1.0" encoding="iso-8859-1"?>
 
2
<!DOCTYPE html
 
3
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
 
4
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 
5
<!-- /tmp/qt-4.0.0-espenr-1119621036935/qt-x11-opensource-desktop-4.0.0/doc/src/examples/calculatorform.qdoc -->
 
6
<head>
 
7
    <title>Qt 4.0: Calculator Form Example</title>
 
8
    <style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }
 
9
a:link { color: #004faf; text-decoration: none }
 
10
a:visited { color: #672967; text-decoration: none }
 
11
td.postheader { font-family: sans-serif }
 
12
tr.address { font-family: sans-serif }
 
13
body { background: #ffffff; color: black; }</style>
 
14
</head>
 
15
<body>
 
16
<table border="0" cellpadding="0" cellspacing="0" width="100%">
 
17
<tr>
 
18
<td align="left" valign="top" width="32"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></td>
 
19
<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="annotated.html"><font color="#004faf">Annotated</font></a>&nbsp;&middot; <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">Functions</font></a></td>
 
20
<td align="right" valign="top" width="230"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></td></tr></table><h1 align="center">Calculator Form Example</h1>
 
21
<p>Files:</p>
 
22
<ul>
 
23
<li><a href="designer-calculatorform-calculatorform-cpp.html">designer/calculatorform/calculatorform.cpp</a></li>
 
24
<li><a href="designer-calculatorform-calculatorform-h.html">designer/calculatorform/calculatorform.h</a></li>
 
25
<li><a href="designer-calculatorform-main-cpp.html">designer/calculatorform/main.cpp</a></li>
 
26
</ul>
 
27
<p>The Calculator Form Example shows how to use a form created with <i>Qt Designer</i> in an application by using the user interface information from a <a href="qwidget.html">QWidget</a> subclass. We use <a href="designer-using-a-component.html#a-dialog-with-auto-connect">uic's auto-connection</a> feature to automatically connect signals from widgets on the form to slots in our code.</p>
 
28
<p>The example presents two spin boxes that are used to input integer values and a label that shows their sum. Whenever either of the spin boxes are updated, the signal-slot connections between the widgets and the form ensure that the label is also updated.</p>
 
29
<a name="preparation"></a>
 
30
<h2>Preparation</h2>
 
31
<p>The user interface for this example is designed completely using <i>Qt Designer</i>. The result is a .ui file describing the form, the widgets used, any signal-slot connections between them, and other standard user interface properties.</p>
 
32
<p>To ensure that the example can use this file, we need to include a <tt>FORMS</tt> declaration in the example's project file:</p>
 
33
<pre>&nbsp;   FORMS       = calculatorform.ui</pre>
 
34
<p>When the project is built, <tt>uic</tt> will create a header file that lets us construct the form.</p>
 
35
<a name="calculatorform-class-definition"></a>
 
36
<h2>CalculatorForm Class Definition</h2>
 
37
<p>The <tt>CalculatorForm</tt> class uses the user interface described in the <tt>calculatorform.ui</tt> file. To access the form and its contents, we need to include the <tt>ui_calculatorform.h</tt> header file created by <tt>uic</tt> during the build process:</p>
 
38
<pre>&nbsp;   #include &quot;ui_calculatorform.h&quot;</pre>
 
39
<p>We define the <tt>CalculatorForm</tt> class by subclassing <a href="qwidget.html">QWidget</a> because the form itself is based on <a href="qwidget.html">QWidget</a>:</p>
 
40
<pre>&nbsp;   class CalculatorForm : public QWidget
 
41
    {
 
42
        Q_OBJECT
 
43
 
 
44
    public:
 
45
        CalculatorForm(QWidget *parent = 0);
 
46
 
 
47
    private slots:
 
48
        void on_inputSpinBox1_valueChanged(int value);
 
49
        void on_inputSpinBox2_valueChanged(int value);
 
50
 
 
51
    private:
 
52
        Ui::CalculatorForm ui;
 
53
    };</pre>
 
54
<p>Apart from the constructor, the class contains two private slots that are named according to the auto-connection naming convention required by <tt>uic</tt>. The private <tt>ui</tt> member variable refers to the form, and is used to access the contents of the user interface.</p>
 
55
<a name="calculatorform-class-implementation"></a>
 
56
<h2>CalculatorForm Class Implementation</h2>
 
57
<p>The constructor simply calls the base class's constructor and sets up the form's user interface.</p>
 
58
<pre>&nbsp;   CalculatorForm::CalculatorForm(QWidget *parent)
 
59
        : QWidget(parent)
 
60
    {
 
61
        ui.setupUi(this);
 
62
    }</pre>
 
63
<p>The user interface is set up with the <tt>setupUI()</tt> function. We pass <tt>this</tt> as the argument to this function to use the <tt>CalculatorForm</tt> widget itself as the container for the user interface.</p>
 
64
<p>To automatically connect signals from the spin boxes defined in the user interface, we use the naming convention that indicates which widgets and their signals in the user interface should be connected to each slot. The first slot is called whenever the spin box called &quot;inputSpinBox1&quot; in the user interface emits the <a href="qspinbox.html#valueChanged">valueChanged()</a> signal:</p>
 
65
<pre>&nbsp;   void CalculatorForm::on_inputSpinBox1_valueChanged(int value)
 
66
    {
 
67
        ui.outputWidget-&gt;setText(QString::number(value + ui.inputSpinBox2-&gt;value()));
 
68
    }</pre>
 
69
<p>When this occurs, we use the value supplied by the signal to update the output label by setting its new text directly. We access the output label and the other spin box via the class's private <tt>ui</tt> variable.</p>
 
70
<p>The second slot is called whenever the second spin box, called &quot;inputSpinBox2&quot;, emits the <a href="qspinbox.html#valueChanged">valueChanged()</a> signal:</p>
 
71
<pre>&nbsp;   void CalculatorForm::on_inputSpinBox2_valueChanged(int value)
 
72
    {
 
73
        ui.outputWidget-&gt;setText(QString::number(value + ui.inputSpinBox1-&gt;value()));
 
74
    }</pre>
 
75
<p>In this case, the value from the first spin box is read and combined with the value supplied by the signal. Again, the output label is updated directly via the <tt>ui</tt> variable.</p>
 
76
<p /><address><hr /><div align="center">
 
77
<table width="100%" cellspacing="0" border="0"><tr class="address">
 
78
<td width="30%">Copyright &copy; 2005 <a href="trolltech.html">Trolltech</a></td>
 
79
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
 
80
<td width="30%" align="right"><div align="right">Qt 4.0.0</div></td>
 
81
</tr></table></div></address></body>
 
82
</html>