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