summaryrefslogtreecommitdiffstats
path: root/examples/designer/doc/src/calculatorbuilder.qdoc
blob: eeec5923b8c37d38b4d74081648c017cc5dc8954 (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
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only

/*!
    \example calculatorbuilder
    \examplecategory {Desktop}
    \meta tags {widgets,designer}
    \ingroup examples-designer
    \title Calculator Builder

    \brief Creating a user interface from a \QD form at run-time.

    \image calculatorbuilder-example.webp

    We use the form created in the \l{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{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 calculatorbuilder/CMakeLists.txt 0

    The UI file is loaded from a resource:

    \snippet calculatorbuilder/CMakeLists.txt 1

    For \c qmake:

    \snippet calculatorbuilder/calculatorbuilder.pro 0

    All the other necessary files are declared as usual.

    \section1 Loading the Calculator Form

    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 calculatorbuilder/main.cpp 0

    We create a static helper function that creates a top level
    widget and loads the user interface that we retrieve,
    via a QFile object, from the example's resources:

    \snippet calculatorbuilder/main.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 findChild() template function
    allows us to query widgets in order to find named child widgets.

    \snippet calculatorbuilder/main.cpp 2

    The slot that modifies the output widget provided by the form is defined
    in a similar way to that in the
    \l{calculatorform}{Calculator Form} example, except that we use a
    lambda, capturing the widgets found:

    \snippet calculatorbuilder/main.cpp 3

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

    \snippet calculatorbuilder/main.cpp 4

    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.

    However, loading a form at runtime incurs a runtime cost compared
    to converting it to C++ code using the \l {User Interface Compiler (uic)}
    tool.
*/