1// Copyright (C) 2023 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
5 \page qtquick-how-tos.html
6 \title Qt Quick How-tos
8 This page aims to provide an easily discoverable, useful reference that
9 shows the simplest and best way of performing specific tasks in Qt Quick.
10 Each solution provides QML and/or C++ code snippets where applicable, and
11 every snippet is automatically tested by Qt to ensure they remain
17 \li \l {Call a C++ function from QML when a Button is clicked}
18 \li \l {See which item has active focus}
19 \li \l {Create a time picker like Android's TimePickerDialog}
20 \li \l {Use a C++ enum in JavaScript}
24 \section1 Call a C++ function from QML when a Button is clicked
26 Assuming that the C++ type should be globally available to the QML
27 files in the application, the simplest way is to make it a QML singleton
28 with \l QML_SINGLETON. For example, in the header file, \c backend.h:
30 \snippet how-tos/how-to-cpp-button/backend.h file
34 \snippet how-tos/how-to-cpp-button/backend.cpp file
36 You can then call that function from any QML file:
38 \snippet how-tos/how-to-cpp-button/Main.qml file
40 If the C++ type only needs to be available to a small set of QML files,
41 consider using \l QML_ELEMENT. For more ways of exposing C++ types to QML,
42 see \l {Choosing the Correct Integration Method Between C++ and QML}.
44 This example assumes that the \c Backend type is available in a QML module.
45 With CMake, this is done via \l qt_add_qml_module. For an example that
46 demonstrates this in detail, see \l {Building a QML application}.
49 \section1 See which item has active focus
51 Write a \l {Property change signal handlers}{property change signal handler}
52 for the window's \l {Window::}{activeFocusItem} property:
54 \snippet how-tos/how-to-qml/active-focus-debugging/ActiveFocusDebuggingMain.qml file
56 This will print the item which currently has active focus to the console.
57 To ensure that the output is useful, give each item a descriptive
58 \l {QtObject::}{objectName}.
61 \section1 Create a time picker like Android's TimePickerDialog
63 We've prepared an example that consists of a few
64 \l {https://code.qt.io/cgit/qt/qtdeclarative.git/tree/tests/auto/quick/doc/how-tos/how-to-qml/time-picker/TimeComponentLabel.qml?h=\QtMajorVersion.\QtMinorVersion}
65 {QML files} which demonstrate how to do this. They can be used
66 in your application in the following manner:
68 \snippet how-tos/how-to-qml/time-picker/TimePickerMain.qml file
72 \li \image how-to-time-picker-light.png
73 \caption TimePickerDialog in its light theme.
74 \li \image how-to-time-picker-dark.png
75 \caption TimePickerDialog in its dark theme.
79 \section1 Use a C++ enum in JavaScript
81 To expose a C++ enum to JavaScript (that is, \l QJSEngine, not
82 \l QQmlEngine or \l QQmlApplicationEngine), use
83 \l QJSEngine::newQMetaObject():
85 \quotefromfile how-tos/how-to-cpp-enum-js/tst_how-to-cpp-enum-js.cpp
86 \skipto QJSEngine engine
87 \printuntil setProperty
88 \skipto Backend backend
89 \printuntil backend.load()
91 The enum can then be used from JavaScript:
93 \snippet how-tos/how-to-cpp-enum-js/script.mjs file
95 When using \l QQmlEngine or \l QQmlApplicationEngine, there are easier
97 \l {Choosing the Correct Integration Method Between C++ and QML}
102 \snippet how-tos/how-to-cpp-enum-js/backend.h file
106 \snippet how-tos/how-to-cpp-enum-js/backend.cpp file
108 For more information, see \l {QObject Integration}.