|
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/calculatorbuilder |
|
44 \title Calculator Builder Example |
|
45 |
|
46 The Calculator Builder example shows how to create a user interface from |
|
47 a \QD form at run-time, using the QUiLoader class. |
|
48 |
|
49 \image calculatorbuilder-example.png |
|
50 |
|
51 We use the form created in the \l{designer/calculatorform}{Calculator Form} |
|
52 example to show that the same user interface can be generated when the |
|
53 application is executed or defined when the application is built. |
|
54 |
|
55 \section1 Preparation |
|
56 |
|
57 The \l{designer/calculatorform}{Calculator Form} example defines a user |
|
58 interface that we can use without modification. In this example, we use a |
|
59 \l{The Qt Resource System}{resource file} to contain the \c{calculatorform.ui} |
|
60 file created in the previous example, but it could be stored on disk instead. |
|
61 |
|
62 To generate a form at run time, we need to link the example against the |
|
63 \c QtUiTools module library. The project file we use contains all the |
|
64 necessary information to do this: |
|
65 |
|
66 \snippet examples/designer/calculatorbuilder/calculatorbuilder.pro 0 |
|
67 |
|
68 All the other necessary files are declared as usual. |
|
69 |
|
70 \section1 CalculatorForm Class Definition |
|
71 |
|
72 The \c CalculatorForm class defines the widget used to host the form's |
|
73 user interface: |
|
74 |
|
75 \snippet examples/designer/calculatorbuilder/calculatorform.h 0 |
|
76 |
|
77 Note that we do not need to include a header file to describe the user |
|
78 interface. We only define two public slots, using the auto-connection |
|
79 naming convention required by \c uic, and declare private variables |
|
80 that we will use to access widgets provided by the form after they are |
|
81 constructed. |
|
82 |
|
83 \section1 CalculatorForm Class Implementation |
|
84 |
|
85 We will need to use the QUiLoader class that is provided by the |
|
86 \c libQtUiTools library, so we first ensure that we include the header |
|
87 file for the module: |
|
88 |
|
89 \snippet examples/designer/calculatorbuilder/calculatorform.cpp 0 |
|
90 |
|
91 The constructor uses a form loader object to construct the user |
|
92 interface that we retrieve, via a QFile object, from the example's |
|
93 resources: |
|
94 |
|
95 \snippet examples/designer/calculatorbuilder/calculatorform.cpp 1 |
|
96 |
|
97 By including the user interface in the example's resources, we ensure |
|
98 that it will be present when the example is run. The \c{loader.load()} |
|
99 function takes the user interface description contained in the file |
|
100 and constructs the form widget as a child widget of the \c{CalculatorForm}. |
|
101 |
|
102 We are interested in three widgets in the generated user interface: |
|
103 two spin boxes and a label. For convenience, we retrieve pointers to |
|
104 these widgets from the widget that was constructed by the \c FormBuilder, |
|
105 and we record them for later use. The \c qFindChild() template function |
|
106 allows us to query widgets in order to find named child widgets. |
|
107 |
|
108 \snippet examples/designer/calculatorbuilder/calculatorform.cpp 2 |
|
109 |
|
110 The widgets created by the form loader need to be connected to the |
|
111 specially-named slots in the \c CalculatorForm object. We use Qt's |
|
112 meta-object system to enable these connections: |
|
113 |
|
114 \snippet examples/designer/calculatorbuilder/calculatorform.cpp 3 |
|
115 |
|
116 The form widget is added to a layout, and the window title is set: |
|
117 |
|
118 \snippet examples/designer/calculatorbuilder/calculatorform.cpp 4 |
|
119 |
|
120 The two slots that modify widgets provided by the form are defined |
|
121 in a similar way to those in the \l{designer/calculatorform}{Calculator |
|
122 Form} example, except that we read the values from the spin boxes and |
|
123 write the result to the output widget via the pointers we recorded in |
|
124 the constructor: |
|
125 |
|
126 \snippet examples/designer/calculatorbuilder/calculatorform.cpp 5 |
|
127 \codeline |
|
128 \snippet examples/designer/calculatorbuilder/calculatorform.cpp 7 |
|
129 |
|
130 The advantage of this approach is that we can replace the form when the |
|
131 application is run, but we can still manipulate the widgets it contains |
|
132 as long as they are given appropriate names. |
|
133 */ |