Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qtquick-how-tos.qdoc
Go to the documentation of this file.
1// Copyright (C) 2023 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4/*!
5 \page qtquick-how-tos.html
6 \title Qt Quick How-tos
7
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
12 functional.
13
14 How do I:
15
16 \list
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}
21 \endlist
22
23
24 \section1 Call a C++ function from QML when a Button is clicked
25
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:
29
30 \snippet how-tos/how-to-cpp-button/backend.h file
31
32 \c backend.cpp:
33
34 \snippet how-tos/how-to-cpp-button/backend.cpp file
35
36 You can then call that function from any QML file:
37
38 \snippet how-tos/how-to-cpp-button/Main.qml file
39
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}.
43
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}.
47
48
49 \section1 See which item has active focus
50
51 Write a \l {Property change signal handlers}{property change signal handler}
52 for the window's \l {Window::}{activeFocusItem} property:
53
54 \snippet how-tos/how-to-qml/active-focus-debugging/ActiveFocusDebuggingMain.qml file
55
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}.
59
60
61 \section1 Create a time picker like Android's TimePickerDialog
62
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:
67
68 \snippet how-tos/how-to-qml/time-picker/TimePickerMain.qml file
69
70 \table
71 \row
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.
76 \endtable
77
78
79 \section1 Use a C++ enum in JavaScript
80
81 To expose a C++ enum to JavaScript (that is, \l QJSEngine, not
82 \l QQmlEngine or \l QQmlApplicationEngine), use
83 \l QJSEngine::newQMetaObject():
84
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()
90
91 The enum can then be used from JavaScript:
92
93 \snippet how-tos/how-to-cpp-enum-js/script.mjs file
94
95 When using \l QQmlEngine or \l QQmlApplicationEngine, there are easier
96 options; see
97 \l {Choosing the Correct Integration Method Between C++ and QML}
98 for more information.
99
100 \c backend.h:
101
102 \snippet how-tos/how-to-cpp-enum-js/backend.h file
103
104 \c backend.cpp:
105
106 \snippet how-tos/how-to-cpp-enum-js/backend.cpp file
107
108 For more information, see \l {QObject Integration}.
109*/