// 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 {Screenshot showing a calculator
           application that adds two numbers and outputs the sum}

    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 class:

    \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 \l{QUiLoader::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.
*/
