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