summaryrefslogtreecommitdiffstats
path: root/doc/src/examples/calculatorbuilder.qdoc
blob: dae70be6c47655f80eb6956b5f6e110ce0f5ba16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the documentation of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:FDL$
** Commercial Usage
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in a
** written agreement between you and Nokia.
**
** GNU Free Documentation License
** Alternatively, this file may be used under the terms of the GNU Free
** Documentation License version 1.3 as published by the Free Software
** Foundation and appearing in the file included in the packaging of this
** file.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
** $QT_END_LICENSE$
**
****************************************************************************/

/*!
    \example designer/calculatorbuilder
    \title Calculator Builder Example

    The Calculator Builder example shows how to create a user interface from
    a \QD form at run-time, using the QUiLoader class.

    \image calculatorbuilder-example.png

    We use the form created in the \l{designer/calculatorform}{Calculator Form}
    example to show that the same user interface can be generated when the
    application is executed or defined when the application is built.

    \section1 Preparation

    The \l{designer/calculatorform}{Calculator Form} example defines a user
    interface that we can use without modification. In this example, we use a
    \l{The Qt Resource System}{resource file} to contain the \c{calculatorform.ui}
    file created in the previous example, but it could be stored on disk instead.

    To generate a form at run time, we need to link the example against the
    \c QtUiTools module library. The project file we use contains all the
    necessary information to do this:

    \snippet examples/designer/calculatorbuilder/calculatorbuilder.pro 0

    All the other necessary files are declared as usual.

    \section1 CalculatorForm Class Definition

    The \c CalculatorForm class defines the widget used to host the form's
    user interface:

    \snippet examples/designer/calculatorbuilder/calculatorform.h 0

    Note that we do not need to include a header file to describe the user
    interface. We only define two public slots, using the auto-connection
    naming convention required by \c uic, and declare private variables
    that we will use to access widgets provided by the form after they are
    constructed.

    \section1 CalculatorForm Class Implementation

    We will need to use the QUiLoader class that is provided by the
    \c libQtUiTools library, so we first ensure that we include the header
    file for the module:

    \snippet examples/designer/calculatorbuilder/calculatorform.cpp 0

    The constructor uses a form loader object to construct the user
    interface that we retrieve, via a QFile object, from the example's
    resources:

    \snippet examples/designer/calculatorbuilder/calculatorform.cpp 1

    By including the user interface in the example's resources, we ensure
    that it will be present when the example is run. The \c{loader.load()}
    function takes the user interface description contained in the file
    and constructs the form widget as a child widget of the \c{CalculatorForm}.

    We are interested in three widgets in the generated user interface:
    two spin boxes and a label. For convenience, we retrieve pointers to
    these widgets from the widget that was constructed by the \c FormBuilder,
    and we record them for later use. The \c qFindChild() template function
    allows us to query widgets in order to find named child widgets.

    \snippet examples/designer/calculatorbuilder/calculatorform.cpp 2

    The widgets created by the form loader need to be connected to the
    specially-named slots in the \c CalculatorForm object. We use Qt's
    meta-object system to enable these connections:

    \snippet examples/designer/calculatorbuilder/calculatorform.cpp 3

    The form widget is added to a layout, and the window title is set:

    \snippet examples/designer/calculatorbuilder/calculatorform.cpp 4

    The two slots that modify widgets provided by the form are defined
    in a similar way to those in the \l{designer/calculatorform}{Calculator
    Form} example, except that we read the values from the spin boxes and
    write the result to the output widget via the pointers we recorded in
    the constructor:

    \snippet examples/designer/calculatorbuilder/calculatorform.cpp 5
    \codeline
    \snippet examples/designer/calculatorbuilder/calculatorform.cpp 7

    The advantage of this approach is that we can replace the form when the
    application is run, but we can still manipulate the widgets it contains
    as long as they are given appropriate names.
*/