// Copyright (C) 2026 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only

/*!
    \page creator-debugging-qml.html
    \previouspage creator-reference.html

    \ingroup creator-reference-debugger

    \title Debugging Qt Quick projects

    \summary {Debug JavaScript functions, execute JavaScript expressions to get
    information about the state of the UI, and inspect QML properties and
    JavaScript variables, as well as change their values temporarily at runtime.}

    For an example of how to debug Qt Quick Projects, see
    \l{Tutorial: Qt Quick debugging}.

    \section1 Setting up QML debugging

    The process of setting up debugging for Qt Quick projects depends on the
    \e type of the project: \uicontrol {Qt Quick UI Prototype} or
    \uicontrol {Qt Quick Application}, and the Qt version.

    \section2 Debugging Qt Quick UI projects

    To debug Qt Quick UI projects (\c .qmlproject), select \uicontrol Automatic
    or \uicontrol Enabled in \uicontrol{Run Settings} >
    \uicontrol {Debugger Settings} > \uicontrol {QML debugger}.

    \image {qtquick-debugger-settings.webp} {Debugger settings section in Run Settings}

    \section2 Debugging Qt Quick applications

    To debug Qt Quick Applications:

        \list 1

        \li To create a build configuration that supports QML debugging,
            go to \uicontrol {Projects} > \uicontrol {Build Settings}.

        \li In \uicontrol {QML debugging and profiling}, select \uicontrol Enable.

            \image {qtcreator-build-settings-cmake-configure.webp} {Build settings for a CMake project}

            \note Debugging requires opening a socket at a TCP port,
            which presents a security risk. Anyone on the Internet could connect
            to the application that you are debugging and execute any JavaScript
            functions. Therefore, you must make sure that the port is properly
            protected by a firewall.

        \li In \uicontrol {Run Settings} > \uicontrol {Debugger settings} >
            \uicontrol {QML debugger}, select \uicontrol Automatic or
            \uicontrol Enabled to enable QML debugging for running applications.

            To debug both the C++ and QML parts of your application at the same
            time, also select \uicontrol Automatic or \uicontrol Enabled in
            \uicontrol {C++ debugger}.

        \li Go to \uicontrol Build, and select \uicontrol {Rebuild Project} to
            clean and rebuild the project.

        \li To debug applications on \l{glossary-device}{devices}, check that
            Qt libraries are installed on the device and
            \l{Running applications}{select the corresponding kit for the device}
            before you start debugging.

    \endlist

    \note The \c qmltooling plugins that are required for debugging are
    automatically installed during \QC and Qt installation. Do not delete
    them if you plan to debug QML applications.

    \section2 Using default values

    You can enable or disable QML debugging globally in \preferences >
    \uicontrol {Build & Run} > \uicontrol {Default Build Properties}.

    \image {qtcreator-build-settings-default.webp} {Default Build Properties tab in Build & Run preferences}

    The value of the \uicontrol {QML debugging} field determines what happens
    when creating new build configurations. The values \uicontrol Enable
    and \uicontrol Disable explicitly set QML debugging for the new build
    configuration to that value, regardless of what type of build
    configuration you create.

    \uicontrol {Use Project Default} makes the values depend on the type of
    build configuration: \uicontrol Debug, \uicontrol Profile, or
    \uicontrol Release. When you use \l {Building projects with CMake}{CMake} or
    \l {Building projects with qmake}{qmake} as a build system, debug and profile
    build configurations have QML debugging enabled. However, release build
    configurations do not include QML debugging because the debugging feature
    makes applications vulnerable.

    The \uicontrol {Default} option in \uicontrol {Projects} >
    \uicontrol {Build Settings} > \uicontrol {QML debugging and profiling}
    keeps existing, already configured CMake build directories intact. Also, it
    enables you to import an existing build into \QC without enforced changes,
    so that you don't have to worry about a complete rebuild of the project, for
    example. Even if you later change the configuration of the build outside of
    \QC, it will be kept as you want it.

    There are some known issues in the interaction between the global setting
    in \preferences > \uicontrol {Build & Run} >
    \uicontrol {Default Build Properties} and the build configuration.
    For example, for qmake the global setting only affects build configurations
    that are automatically created when enabling a kit. Also, CMake ignores the
    global setting.

    \section1 Starting QML debugging

    To start the application, choose \uicontrol Debug > \uicontrol {Start Debugging}
    > \uicontrol {Start Debugging of Startup Project} or select \key F5. Once the
    application starts running, it behaves and performs as usual. You can then
    perform the following tasks:

    \list

        \li Debug JavaScript functions

        \li Execute JavaScript expressions to get information about the state of
            the application

        \li Inspect QML properties and JavaScript variables and change them
            temporarily at runtime

    \endlist

    To debug already running applications:

    \list 1

        \li Build the application by using the appropriate configuration
            parameters (if you build the application with \QC, it automatically
            uses the correct configuration):
            \list
                \li When using CMake, the
                    \l{CMake: target_compile_definitions command}
                    {target_compile_definitions} command is defined
                    in the \c CMakeLists.txt file:
                    \c {target_compile_definitions(myapp PRIVATE QT_QML_DEBUG)}

                    Where \e myapp is the application to debug.
                \li When using qmake, the following value is defined for the
                    \l CONFIG property in the \c .pro file:
                    \c {CONFIG += qml_debug}
            \endlist

        \li Start the application with the following arguments:

            \c {-qmljsdebugger=port:<port>[,host:<ip address>][,block]}

            Where \c port (mandatory) specifies the debugging port,
            \c {ip address} (optional) specifies the IP address of the host
            where the application is running, and \c block (optional) prevents
            the application from running until the debug client connects to the
            server. This enables debugging from the start.

            \note Setting breakpoints is only possible if the application is started with block mode.

        \li Go to \uicontrol Debug > \uicontrol {Start Debugging} >
            \uicontrol {Attach to QML Port}.

            Choose the kit configured for the device where the application to
            be debugged is running. The port number to use is displayed in the
            standard output when the application starts.

    \endlist

    \section1 Debugging JavaScript functions

    You can use the \QC \uicontrol Debug mode to inspect the state of your
    application while debugging. You can interact with the debugger by:

    \list

        \li \l{Viewing call stack trace}{Viewing call stack trace}

        \li \l{Setting breakpoints}{Setting breakpoints}

        \li \l{Local variables and function parameters}
              {Viewing local variables and function parameters}

        \li \l{Evaluating expressions}

    \endlist

    \section1 Inspecting items

    While the application is running, you can use the \uicontrol {Locals}
    view to explore the QML item structure.

    \image {qml-observer-view.png} {QML item tree in the Locals view}

    To keep the application visible while you interact with the debugger, select
    \uicontrol Debug > \uicontrol {Show Application on Top}.

    You can view a QML item in the \uicontrol {Locals} view in the following
    ways:

    \list

        \li Expand the item in the object tree.

        \li Select the item in the code editor.

        \li Go to \uicontrol Debug > \uicontrol Select to activate selection
            mode and then select an item in the running application.

    \endlist

    To change property values temporarily, without editing the source,
    double-click them and enter the new values. You can view the results in the
    running application.

    \section1 Inspecting user interfaces

    When you debug complex applications, you can jump to the position in code
    where an item is defined.

    In the selection mode, you can select items in the running
    application to jump to their definitions in the code. The properties of the
    selected item are displayed in the \uicontrol {Locals} view.

    You can also view the item hierarchy in the running application:

    Double-click an item in the running application to cycle through the item
    stack at the cursor position.

    To switch out of the selection mode, toggle the \uicontrol Select menu item.

    To move the application running in \QQV to the front, select
    \uicontrol Debug > \uicontrol {Show Application on Top}.

    \section1 Executing JavaScript expressions

    When the application is interrupted by a breakpoint, you can use the
    \uicontrol {QML Debugger Console} to execute JavaScript expressions in the
    current context. To open it, choose \uicontrol View > \uicontrol Output >
    \uicontrol {QML Debugger Console}.

    \image {qml-script-console.png} {QML Debugger Console}

    You can change property values temporarily, without editing the source, and
    view the results in the running application. You can change the property
    values permanently in code.

    \section1 Applying QML changes at runtime

    When you change property values in the \uicontrol {QML Debugger Console} or
    in the \uicontrol Locals or \uicontrol Expression view, they are immediately
    updated in the running application, but not in the source code.

    \sa {Debug}{How to: Debug}, {Debugging}, {Debuggers}, {Managing kits}
*/
