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

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

    \brief Using a form created with \QD in an application.

    The Calculator Form Example shows how to use a form created with
    \QD in an application by using the user interface information from
    a QWidget subclass.

    \image calculatorform-example.webp Screenshot of the Calculator Form example

    The example presents two spin boxes that are used to input integer values
    and a label that shows their sum. Whenever either of the spin boxes are
    updated, the signal-slot connections between the widgets and the form
    ensure that the label is also updated.

    \section1 Preparation

    The user interface for this example is designed completely using \QD. The
    result is a UI file describing the form, the widgets used, any signal-slot
    connections between them, and other standard user interface properties.

    To ensure that the example can use this file, we enable the \c CMAKE_AUTOUIC
    feature and list the UI file in the source files:

    \snippet calculatorform/CMakeLists.txt 0
    \codeline
    \snippet calculatorform/CMakeLists.txt 1

    For \c qmake, we need to include a \c FORMS declaration in the example's project file:

    \snippet calculatorform/calculatorform.pro 1

    When the project is built, \c uic will create a header file that lets us
    construct the form.

    \section1 CalculatorForm Class Definition

    The \c CalculatorForm class uses the user interface described in the
    \c calculatorform.ui file. To access the form and its contents, we need
    to include the \c ui_calculatorform.h header file created by \c uic
    during the build process:

    \snippet calculatorform/calculatorform.h 0

    We define the \c CalculatorForm class by subclassing QWidget because the
    form itself is based on QWidget:

    \snippet calculatorform/calculatorform.h 1

    Apart from the constructor, the class contains a private slot
    \c updateResult() that performs the calculation and updates the output
    widget accordingly.
    The private \c ui member variable refers to the form, and is used to
    access the contents of the user interface.

    \section1 CalculatorForm Class Implementation

    The constructor simply calls the base class's constructor,
    sets up the form's user interface and connects
    the signals \l{QSpinBox::valueChanged()} to the slot \c updateResult().

    \snippet calculatorform/calculatorform.cpp 0

    The user interface is set up with the \c setupUI() function. We pass
    \c this as the argument to this function to use the \c CalculatorForm
    widget itself as the container for the user interface.

    Slot \c updateResult() adds the values and sets the result on
    the output widget:

    \snippet calculatorform/calculatorform.cpp 1

    It is called whenever the value of one of the spin boxes changes.
*/