0
|
1 |
/****************************************************************************
|
|
2 |
**
|
|
3 |
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
4 |
** All rights reserved.
|
|
5 |
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
6 |
**
|
|
7 |
** This file is part of the documentation of the Qt Toolkit.
|
|
8 |
**
|
|
9 |
** $QT_BEGIN_LICENSE:LGPL$
|
|
10 |
** No Commercial Usage
|
|
11 |
** This file contains pre-release code and may not be distributed.
|
|
12 |
** You may use this file in accordance with the terms and conditions
|
|
13 |
** contained in the Technology Preview License Agreement accompanying
|
|
14 |
** this package.
|
|
15 |
**
|
|
16 |
** GNU Lesser General Public License Usage
|
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
18 |
** General Public License version 2.1 as published by the Free Software
|
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
20 |
** packaging of this file. Please review the following information to
|
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
23 |
**
|
|
24 |
** In addition, as a special exception, Nokia gives you certain additional
|
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception
|
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
27 |
**
|
|
28 |
** If you have questions regarding the use of this file, please contact
|
|
29 |
** Nokia at qt-info@nokia.com.
|
|
30 |
**
|
|
31 |
**
|
|
32 |
**
|
|
33 |
**
|
|
34 |
**
|
|
35 |
**
|
|
36 |
**
|
|
37 |
**
|
|
38 |
** $QT_END_LICENSE$
|
|
39 |
**
|
|
40 |
****************************************************************************/
|
|
41 |
|
|
42 |
/*!
|
|
43 |
\example designer/calculatorform
|
|
44 |
\title Calculator Form Example
|
|
45 |
|
|
46 |
The Calculator Form Example shows how to use a form created with
|
|
47 |
\QD in an application by using the user interface information from
|
|
48 |
a QWidget subclass. We use \l{Using a Designer UI File in Your Application}
|
|
49 |
{uic's auto-connection} feature to automatically connect signals
|
|
50 |
from widgets on the form to slots in our code.
|
|
51 |
|
|
52 |
\image calculatorform-example.png Screenshot of the Calculator Form example
|
|
53 |
|
|
54 |
The example presents two spin boxes that are used to input integer values
|
|
55 |
and a label that shows their sum. Whenever either of the spin boxes are
|
|
56 |
updated, the signal-slot connections between the widgets and the form
|
|
57 |
ensure that the label is also updated.
|
|
58 |
|
|
59 |
\section1 Preparation
|
|
60 |
|
|
61 |
The user interface for this example is designed completely using \QD. The
|
|
62 |
result is a UI file describing the form, the widgets used, any signal-slot
|
|
63 |
connections between them, and other standard user interface properties.
|
|
64 |
|
|
65 |
To ensure that the example can use this file, we need to include a \c FORMS
|
|
66 |
declaration in the example's project file:
|
|
67 |
|
|
68 |
\snippet examples/designer/calculatorform/calculatorform.pro 1
|
|
69 |
|
|
70 |
When the project is built, \c uic will create a header file that lets us
|
|
71 |
construct the form.
|
|
72 |
|
|
73 |
\section1 CalculatorForm Class Definition
|
|
74 |
|
|
75 |
The \c CalculatorForm class uses the user interface described in the
|
|
76 |
\c calculatorform.ui file. To access the form and its contents, we need
|
|
77 |
to include the \c ui_calculatorform.h header file created by \c uic
|
|
78 |
during the build process:
|
|
79 |
|
|
80 |
\snippet examples/designer/calculatorform/calculatorform.h 0
|
|
81 |
|
|
82 |
We define the \c CalculatorForm class by subclassing QWidget because the
|
|
83 |
form itself is based on QWidget:
|
|
84 |
|
|
85 |
\snippet examples/designer/calculatorform/calculatorform.h 1
|
|
86 |
|
|
87 |
Apart from the constructor, the class contains two private slots that
|
|
88 |
are named according to the auto-connection naming convention required
|
|
89 |
by \c uic.
|
|
90 |
The private \c ui member variable refers to the form, and is used to
|
|
91 |
access the contents of the user interface.
|
|
92 |
|
|
93 |
\section1 CalculatorForm Class Implementation
|
|
94 |
|
|
95 |
The constructor simply calls the base class's constructor and
|
|
96 |
sets up the form's user interface.
|
|
97 |
|
|
98 |
\snippet examples/designer/calculatorform/calculatorform.cpp 0
|
|
99 |
|
|
100 |
The user interface is set up with the \c setupUI() function. We pass
|
|
101 |
\c this as the argument to this function to use the \c CalculatorForm
|
|
102 |
widget itself as the container for the user interface.
|
|
103 |
|
|
104 |
To automatically connect signals from the spin boxes defined in the
|
|
105 |
user interface, we use the naming convention that indicates which
|
|
106 |
widgets and their signals in the user interface should be connected
|
|
107 |
to each slot. The first slot is called whenever the spin box called
|
|
108 |
"inputSpinBox1" in the user interface emits the
|
|
109 |
\l{QSpinBox::valueChanged()}{valueChanged()} signal:
|
|
110 |
|
|
111 |
\snippet examples/designer/calculatorform/calculatorform.cpp 1
|
|
112 |
|
|
113 |
When this occurs, we use the value supplied by the signal to update the
|
|
114 |
output label by setting its new text directly. We access the output label
|
|
115 |
and the other spin box via the class's private \c ui variable.
|
|
116 |
|
|
117 |
The second slot is called whenever the second spin box, called
|
|
118 |
"inputSpinBox2", emits the \l{QSpinBox::valueChanged()}{valueChanged()}
|
|
119 |
signal:
|
|
120 |
|
|
121 |
\snippet examples/designer/calculatorform/calculatorform.cpp 2
|
|
122 |
|
|
123 |
In this case, the value from the first spin box is read and combined
|
|
124 |
with the value supplied by the signal. Again, the output label is
|
|
125 |
updated directly via the \c ui variable.
|
|
126 |
*/
|