1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information ([email protected])
|
---|
5 | **
|
---|
6 | ** This file is part of the documentation of the Qt Toolkit.
|
---|
7 | **
|
---|
8 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
9 | ** Commercial Usage
|
---|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
11 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
12 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
13 | ** a written agreement between you and Nokia.
|
---|
14 | **
|
---|
15 | ** GNU Lesser General Public License Usage
|
---|
16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
17 | ** General Public License version 2.1 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
19 | ** packaging of this file. Please review the following information to
|
---|
20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
22 | **
|
---|
23 | ** In addition, as a special exception, Nokia gives you certain
|
---|
24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
---|
25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
---|
26 | ** package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you are unsure which license is appropriate for your use, please
|
---|
37 | ** contact the sales department at [email protected].
|
---|
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 | */
|
---|