Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
Loading...
Searching...
No Matches
qt-for-harmonyos-platform-api.qdoc
Go to the documentation of this file.
1
// Copyright (C) 2026 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4
/*!
5
\page qt-for-harmonyos-platform-api.html
6
\title Qt for HarmonyOS Platform API
7
\brief Using HarmonyOS platform APIs from a Qt application.
8
9
\note Qt's HarmonyOS platform API is in Technology Preview and may
10
change in future releases.
11
12
A Qt application can call HarmonyOS ArkTS APIs directly. This
13
documentation calls them platform APIs. Use them for features that Qt
14
does not cover, such as showing the system share sheet, making an
15
in-app purchase, or refreshing the application's home-screen widgets.
16
Qt does not wrap these APIs.
17
18
On HarmonyOS, the ArkTS runtime and the platform UI run on the main
19
thread of the process. This documentation refers to this thread as the
20
JS thread, and to the objects of the ArkTS runtime as JS objects. Qt
21
runs the application on a separate thread, called the Qt thread.
22
23
Qt provides the following classes and functions for calling platform
24
APIs:
25
26
\list
27
\li QNativeInterface::QHarmonyApplication runs code on the JS thread,
28
where the platform APIs are available, and brings results back to
29
the Qt thread. The code receives a QHarmonyJsContext.
30
\li QHarmonyJsContext provides the Node-API environment and loads JS
31
modules by name.
32
\li QHarmonyAppJsContext gives access to application-level JS objects,
33
such as the default \c UIAbility instance.
34
\li QHarmonyJsObjectRef keeps a JS object alive. You can store, copy,
35
and compare the reference on any thread, but you can access the
36
object only on the JS thread.
37
\li QNativeInterface::QHarmonyWindow provides the ability that shows a
38
QWindow, and the \c window.Window that the QWindow is shown in.
39
\li The QtHarmony namespace contains free functions for code that runs
40
on the JS thread, such as conversions between QJsonObject and JS
41
objects.
42
\endlist
43
44
Qt's HarmonyOS platform API uses the C Node-API types \c napi_env and
45
\c napi_value. They are part of the HarmonyOS SDK and have a stable ABI.
46
Node-API is a low-level, weakly typed C API that requires manual error
47
handling and resource management. It is strongly recommended that
48
applications use
49
\l {https://github.com/nodejs/node-addon-api}{node-addon-api}, the
50
header-only C++ wrapper for Node-API that the Node.js project publishes.
51
Its types convert to and from the C types at no cost. The snippet in
52
the next section uses it.
53
54
\section1 The Two Threads
55
56
The Qt thread is the thread of the QCoreApplication object, which runs
57
the Qt event loop. Platform APIs are available only on the JS thread. To
58
run code there, pass it as a task to
59
QNativeInterface::QHarmonyApplication::runOnJsThread():
60
61
\code
62
const QJsonObject vibrationEffect{
63
{"type", "time"},
64
{"duration", 200},
65
};
66
const QJsonObject vibrationAttribute{
67
{"usage", "touch"},
68
};
69
70
// ...
71
72
QNativeInterface::QHarmonyApplication::runOnJsThread(
73
[vibrationEffect, vibrationAttribute](QHarmonyJsContext &jsContext) {
74
Napi::Env env(jsContext.env());
75
Napi::Object vibratorModule(env, jsContext.tryImportModule("@ohos.vibrator"));
76
if (vibratorModule.IsEmpty())
77
return;
78
79
Napi::Object jsVibrationEffect(env, QtHarmony::mapQJsonObjectToJsObject(env, vibrationEffect));
80
Napi::Object jsVibrationAttribute(env, QtHarmony::mapQJsonObjectToJsObject(env, vibrationAttribute));
81
82
auto startVibrationFunc = vibratorModule.Get("startVibration").As<Napi::Function>();
83
auto startVibrationPromise = startVibrationFunc.Call(vibratorModule, {jsVibrationEffect, jsVibrationAttribute}).As<Napi::Object>();
84
startVibrationPromise.Get("then").As<Napi::Function>().Call(
85
startVibrationPromise,
86
{
87
Napi::Function::New(
88
env,
89
[](const Napi::CallbackInfo &) {
90
qDebug() << "startVibration() succeeded";
91
}),
92
Napi::Function::New(
93
env,
94
[](const Napi::CallbackInfo &info) {
95
qWarning() << "startVibration() failed:" << QString::fromStdString(info[0].ToString());
96
}),
97
});
98
});
99
\endcode
100
101
Code that runs on the JS thread must follow the rules listed in the
102
QNativeInterface::QHarmonyApplication documentation. To send results
103
back to the Qt thread, use
104
QNativeInterface::QHarmonyApplication::runOnQtThread(). To pass JS data
105
to the Qt thread, convert it to a QJsonObject with
106
QtHarmony::mapJsObjectToQJsonObject(), or keep it in a
107
QHarmonyJsObjectRef.
108
109
\section1 JS Callbacks
110
111
Asynchronous platform APIs return promises or take callbacks. To handle
112
a promise or to pass a callback, create a function with
113
\c napi_create_function(). Such a function receives the \c napi_env, but
114
not a QHarmonyJsContext. To get the context, call
115
QHarmonyJsContext::fromEnv(). These functions run on the JS thread and
116
follow the same rules as tasks passed to
117
QNativeInterface::QHarmonyApplication::runOnJsThread().
118
119
\section1 Window-Bound Platform APIs
120
121
QHarmonyAppJsContext::defaultAbility() is meant for calls that concern
122
the application as a whole. Some platform APIs are bound to a particular
123
window, for example a picker that opens over the window, or an ability
124
started from it. These APIs need the \c UIAbility instance that shows
125
the window, or the window's \c window.Window.
126
127
To get them, use QNativeInterface::QHarmonyWindow, which is available
128
through \c {QWindow::nativeInterface<QNativeInterface::QHarmonyWindow>()}.
129
It returns both as QHarmonyJsObjectRef handles. You get the handles on
130
the Qt thread and use the objects they refer to on the JS thread.
131
132
\section1 JS Modules
133
134
QHarmonyJsContext::tryImportModule() returns a JS module by name, for
135
example \c {@ohos.app.ability.AbilityConstant}. It loads the module on
136
first use. Enumerations are properties of the module object or of a
137
namespace in it. For example, \c LaunchReason is a property of the
138
\c {@ohos.app.ability.AbilityConstant} module object. Enumeration
139
values are properties of their enumeration.
140
141
Native code cannot load kits, the \c {@kit.*} modules. The ArkTS
142
application template bundled with Qt makes the kit modules that Qt
143
needs available to native code. QHarmonyJsContext::tryImportModule()
144
accepts their names, such as \c {@kit.ShareKit.systemShare}.
145
146
\section1 Platform API Examples
147
148
\list
149
\li \l {Qt for HarmonyOS Restart Application Example}
150
\li \l {Qt for HarmonyOS Notification Example}
151
\li \l {Qt for HarmonyOS Window Settings Example}
152
\li \l {Qt for HarmonyOS Native Window ID Example}
153
\li \l {Qt for HarmonyOS Window Drag Key Frame Example}
154
\endlist
155
*/
qtbase
src
corelib
doc
src
qt-for-harmonyos-platform-api.qdoc
Generated on
for Qt by
1.18.0