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

« back to all changes in this revision

Viewing changes to doc/html/designer-calculatorbuilder.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/calculatorbuilder.qdoc -->
 
6
<head>
 
7
    <title>Qt 4.0: Calculator Builder</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 Builder</h1>
 
21
<p>Files:</p>
 
22
<ul>
 
23
<li><a href="designer-calculatorbuilder-calculatorform-cpp.html">designer/calculatorbuilder/calculatorform.cpp</a></li>
 
24
<li><a href="designer-calculatorbuilder-calculatorform-h.html">designer/calculatorbuilder/calculatorform.h</a></li>
 
25
<li><a href="designer-calculatorbuilder-main-cpp.html">designer/calculatorbuilder/main.cpp</a></li>
 
26
<li><a href="designer-calculatorbuilder-calculatorbuilder-qrc.html">designer/calculatorbuilder/calculatorbuilder.qrc</a></li>
 
27
</ul>
 
28
<p>The Calculator Builder example shows how to create a user interface from a <i>Qt Designer</i> form at runtime, using the <tt>FormBuilder</tt> class.</p>
 
29
<p>We use the form created in the <a href="designer-calculatorform.html">Calculator Form</a> example to show that the same user interface can be generated when the application is executed or defined when the application is built.</p>
 
30
<a name="preparation"></a>
 
31
<h2>Preparation</h2>
 
32
<p>The <a href="designer-calculatorform.html">Calculator Form</a> example defines a user interface that we can use without modification. In this example, we use a <a href="resources.html">resource file</a> to contain the <tt>calculatorform.ui</tt> file created in the previous example, but it could be stored on disk instead.</p>
 
33
<p>To generate a form at run time, we need to link the example against the <tt>libQtDesigner</tt> library. The project file we use contains all the necessary information to do this:</p>
 
34
<pre>&nbsp;   CONFIG      += designer
 
35
 
 
36
    HEADERS     = calculatorform.h
 
37
    SOURCES     = calculatorform.cpp \
 
38
                  main.cpp
 
39
    RESOURCES   = calculatorbuilder.qrc</pre>
 
40
<p>All the other necessary files are declared as usual.</p>
 
41
<a name="calculatorform-class-definition"></a>
 
42
<h2>CalculatorForm Class Definition</h2>
 
43
<p>The <tt>CalculatorForm</tt> class defines the widget used to host the form's user interface:</p>
 
44
<pre>&nbsp;   class CalculatorForm : public QWidget
 
45
    {
 
46
        Q_OBJECT
 
47
 
 
48
    public:
 
49
        CalculatorForm(QWidget *parent = 0);
 
50
 
 
51
    private slots:
 
52
        void on_inputSpinBox1_valueChanged(int value);
 
53
        void on_inputSpinBox2_valueChanged(int value);
 
54
 
 
55
    private:
 
56
        QSpinBox *ui_inputSpinBox1;
 
57
        QSpinBox *ui_inputSpinBox2;
 
58
        QLabel *ui_outputWidget;
 
59
    };</pre>
 
60
<p>Note that we do not need to include a header file to describe the user interface. We only define two public slots, using the auto-connection naming convention required by <tt>uic</tt>, and declare private variables that we will use to access widgets provided by the form after they are constructed.</p>
 
61
<a name="calculatorform-class-implementation"></a>
 
62
<h2>CalculatorForm Class Implementation</h2>
 
63
<p>We will need to use the <tt>FormBuilder</tt> class that is provided by the <tt>libQtDesigner</tt> library, so we first ensure that we include the header file for it:</p>
 
64
<pre></pre>
 
65
<p>The constructor uses a <tt>FormBuilder</tt> object to construct the user interface that we retrieve, via a <a href="qfile.html">QFile</a> object, from the example's resources:</p>
 
66
<pre>&nbsp;   CalculatorForm::CalculatorForm(QWidget *parent)
 
67
        : QWidget(parent)
 
68
    {
 
69
        QFormBuilder builder;
 
70
        QFile file(&quot;:/forms/calculatorform.ui&quot;);
 
71
        file.open(QFile::ReadOnly);
 
72
        QWidget *formWidget = builder.load(&amp;file, this);
 
73
        file.close();</pre>
 
74
<p>By including the user interface in the example's resources, we ensure that it will be present when the example is run. The <tt>builder.load()</tt> function takes the user interface description contained in the file and constructs the form widget as a child widget of the <tt>CalculatorForm</tt>.</p>
 
75
<p>We are interested in three widgets in the generated user interface: two spin boxes and a label. For convenience, we retrieve pointers to these widgets from the widget that was constructed by the <tt>FormBuilder</tt>, and we record them for later use. The <tt>qFindChild()</tt> template function allows us to query widgets in order to find named child widgets.</p>
 
76
<p>The widgets created by the <tt>FormBuilder</tt> need to be connected to the specially-named slots in the <tt>CalculatorForm</tt> object. We use Qt's meta-object system to enable these connections:</p>
 
77
<pre></pre>
 
78
<p>The form widget is added to a layout, and the window title is set:</p>
 
79
<pre>&nbsp;       QVBoxLayout *layout = new QVBoxLayout;
 
80
        layout-&gt;addWidget(formWidget);
 
81
        setLayout(layout);
 
82
 
 
83
        setWindowTitle(tr(&quot;Calculator Builder&quot;));
 
84
    }</pre>
 
85
<p>The two slots that modify widgets provided by the form are defined in a similar way to those in the <a href="designer-calculatorform.html">Calculator Form</a> example, except that we read the values from the spin boxes and write the result to the output widget via the pointers we recorded in the constructor:</p>
 
86
<pre>&nbsp;   void CalculatorForm::on_inputSpinBox1_valueChanged(int value)
 
87
    {
 
88
        ui_outputWidget-&gt;setText(QString::number(value + ui_inputSpinBox2-&gt;value()));
 
89
    }
 
90
 
 
91
    void CalculatorForm::on_inputSpinBox2_valueChanged(int value)
 
92
    {
 
93
        ui_outputWidget-&gt;setText(QString::number(value + ui_inputSpinBox1-&gt;value()));
 
94
    }</pre>
 
95
<p>The advantage of this approach is that we can replace the form when the application is run, but we can still manipulate the widgets it contains as long as they are given appropriate names.</p>
 
96
<p /><address><hr /><div align="center">
 
97
<table width="100%" cellspacing="0" border="0"><tr class="address">
 
98
<td width="30%">Copyright &copy; 2005 <a href="trolltech.html">Trolltech</a></td>
 
99
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
 
100
<td width="30%" align="right"><div align="right">Qt 4.0.0</div></td>
 
101
</tr></table></div></address></body>
 
102
</html>