Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
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, and registers delegates
34 for \c UIAbility methods.
35 \li QHarmonyJsObjectRef keeps a JS object alive. You can store, copy,
36 and compare the reference on any thread, but you can access the
37 object only on the JS thread.
38 \li QNativeInterface::QHarmonyWindow provides the ability that shows a
39 QWindow, and the \c window.Window that the QWindow is shown in.
40 \li QNativeInterface::QHarmonyScreen provides the ID of the display
41 that a QScreen represents.
42 \li The QtHarmony namespace contains free functions for code that runs
43 on the JS thread, such as conversions between QJsonObject and JS
44 objects.
45 \endlist
46
47 Qt's HarmonyOS platform API uses the C Node-API types \c napi_env and
48 \c napi_value. They are part of the HarmonyOS SDK and have a stable ABI.
49 Node-API is a low-level, weakly typed C API that requires manual error
50 handling and resource management. It is strongly recommended that
51 applications use
52 \l {https://github.com/nodejs/node-addon-api}{node-addon-api}, the
53 header-only C++ wrapper for Node-API that the Node.js project publishes.
54 Its types convert to and from the C types at no cost. The snippet in
55 the next section uses it.
56
57 \section1 The Two Threads
58
59 The Qt thread is the thread of the QCoreApplication object, which runs
60 the Qt event loop. Platform APIs are available only on the JS thread. To
61 run code there, pass it as a task to
62 QNativeInterface::QHarmonyApplication::runOnJsThread():
63
64 \code
65 const QJsonObject vibrationEffect{
66 {"type", "time"},
67 {"duration", 200},
68 };
69 const QJsonObject vibrationAttribute{
70 {"usage", "touch"},
71 };
72
73 // ...
74
75 QNativeInterface::QHarmonyApplication::runOnJsThread(
76 [vibrationEffect, vibrationAttribute](QHarmonyJsContext &jsContext) {
77 Napi::Env env(jsContext.env());
78 Napi::Object vibratorModule(env, jsContext.tryImportModule("@ohos.vibrator"));
79 if (vibratorModule.IsEmpty())
80 return;
81
82 Napi::Object jsVibrationEffect(env, QtHarmony::mapQJsonObjectToJsObject(env, vibrationEffect));
83 Napi::Object jsVibrationAttribute(env, QtHarmony::mapQJsonObjectToJsObject(env, vibrationAttribute));
84
85 auto startVibrationFunc = vibratorModule.Get("startVibration").As<Napi::Function>();
86 auto startVibrationPromise = startVibrationFunc.Call(vibratorModule, {jsVibrationEffect, jsVibrationAttribute}).As<Napi::Object>();
87 startVibrationPromise.Get("then").As<Napi::Function>().Call(
88 startVibrationPromise,
89 {
90 Napi::Function::New(
91 env,
92 [](const Napi::CallbackInfo &) {
93 qDebug() << "startVibration() succeeded";
94 }),
95 Napi::Function::New(
96 env,
97 [](const Napi::CallbackInfo &info) {
98 qWarning() << "startVibration() failed:" << QString::fromStdString(info[0].ToString());
99 }),
100 });
101 });
102 \endcode
103
104 Code that runs on the JS thread must follow the rules listed in the
105 QNativeInterface::QHarmonyApplication documentation. To send results
106 back to the Qt thread, use
107 QNativeInterface::QHarmonyApplication::runOnQtThread(). To pass JS data
108 to the Qt thread, convert it to a QJsonObject with
109 QtHarmony::mapJsObjectToQJsonObject(), or keep it in a
110 QHarmonyJsObjectRef.
111
112 \section1 JS Callbacks
113
114 Asynchronous platform APIs return promises or take callbacks. To handle
115 a promise or to pass a callback, create a function with
116 \c napi_create_function(). Such a function receives the \c napi_env, but
117 not a QHarmonyJsContext. To get the context, call
118 QHarmonyJsContext::fromEnv(). These functions run on the JS thread and
119 follow the same rules as tasks passed to
120 QNativeInterface::QHarmonyApplication::runOnJsThread().
121
122 \section1 Window-Bound Platform APIs
123
124 QHarmonyAppJsContext::defaultAbility() is meant for calls that concern
125 the application as a whole. Some platform APIs are bound to a particular
126 window, for example a picker that opens over the window, or an ability
127 started from it. These APIs need the \c UIAbility instance that shows
128 the window, or the window's \c window.Window.
129
130 To get them, use QNativeInterface::QHarmonyWindow, which is available
131 through \c {QWindow::nativeInterface<QNativeInterface::QHarmonyWindow>()}.
132 It returns both as QHarmonyJsObjectRef handles. You get the handles on
133 the Qt thread and use the objects they refer to on the JS thread.
134
135 \section1 JS Modules
136
137 QHarmonyJsContext::tryImportModule() returns a JS module by name, for
138 example \c {@ohos.app.ability.AbilityConstant}. It loads the module on
139 first use. Enumerations are properties of the module object or of a
140 namespace in it. For example, \c LaunchReason is a property of the
141 \c {@ohos.app.ability.AbilityConstant} module object. Enumeration
142 values are properties of their enumeration.
143
144 Native code cannot load kits, the \c {@kit.*} modules. The ArkTS
145 application template bundled with Qt makes the kit modules that Qt
146 needs available to native code. QHarmonyJsContext::tryImportModule()
147 accepts their names, such as \c {@kit.ShareKit.systemShare}.
148
149 \section1 UIAbility Delegates
150
151 Some platform callbacks reach only the application's
152 \l {https://developer.huawei.com/consumer/en/doc/harmonyos-references/js-apis-app-ability-uiability}{HarmonyOS UIAbility}
153 class, which the ArkTS application template bundled with Qt implements.
154 \c onNewWant() delivers a Want to a running ability, and
155 \c onContinue() asks whether the ability may be migrated to another
156 device. To receive such calls, register a delegate with
157 QHarmonyAppJsContext::addUiAbilityDelegate(). A delegate is a JS object
158 whose methods Qt calls from the matching \c UIAbility methods, with the
159 \c UIAbility instance as the first argument, followed by the arguments
160 of the \c UIAbility method. The function's documentation lists the
161 forwarded methods and the rules for each.
162
163 Delegate methods run on the JS thread and follow the same rules as
164 tasks passed to QNativeInterface::QHarmonyApplication::runOnJsThread().
165 The handle that addUiAbilityDelegate() returns can be released on any
166 thread. Releasing it on another thread blocks that thread until the JS
167 thread has unregistered the delegate.
168
169 The following class receives new Wants as QJsonObject on the Qt
170 thread. It registers the delegate in its constructor, and the handle is
171 released when the receiver is destroyed:
172
173 \code
174 class WantReceiver : public QObject
175 {
176 public:
177 WantReceiver()
178 {
179 m_delegateHandle = QNativeInterface::QHarmonyApplication::runOnJsThreadAndWait(
180 [self = QPointer<WantReceiver>(this)](QHarmonyJsContext &jsContext) {
181 Napi::Env env(jsContext.env());
182 Napi::Object delegate = Napi::Object::New(env);
183 delegate.Set(
184 "onNewWant",
185 Napi::Function::New(
186 env,
187 [self](const Napi::CallbackInfo &info) {
188 const QJsonObject want = QtHarmony::mapJsObjectToQJsonObject(info.Env(), info[1]);
189 QNativeInterface::QHarmonyApplication::runOnQtThread(
190 [self, want]() {
191 if (self)
192 self->handleNewWant(want);
193 });
194 }));
195 return QHarmonyAppJsContext::fromJsContext(jsContext).addUiAbilityDelegate(delegate);
196 });
197 }
198
199 private:
200 void handleNewWant(const QJsonObject &want);
201
202 std::shared_ptr<void> m_delegateHandle;
203 };
204 \endcode
205
206 \section1 Abilities in Separate Processes
207
208 An application can run its \c UIAbility in a separate process with a
209 name that it chooses, and send later Wants to that process. To do so,
210 start the application's own ability with a Want that has the name in
211 the \c io.qt.appProcessId parameter. Take the \c bundleName,
212 \c moduleName and \c abilityName of the Want from the \c bundleName,
213 \c moduleName and \c name of \c {context.abilityInfo} of
214 QHarmonyAppJsContext::defaultAbility(), and pass the Want to
215 \c {context.startAbility()} of an ability of the application.
216
217 The name is a non-empty string, for example one name per open document,
218 or a fixed name for a single helper process.
219
220 \list
221 \li If no process with that name is running, the system starts a new
222 process of the application. Qt starts the application in it as on a
223 normal launch, and the Want is the launch Want of the default
224 ability in that process.
225 \li If a process with that name is running, the Want goes to that
226 process. If the ability that was started for that name still runs
227 there, Qt passes the Want to the UIAbility delegates as
228 \c onNewWant().
229 \endlist
230
231 A name refers to a process only while that process runs. After the
232 process exits, the same name starts a new process. Qt does not promise
233 that the \c io.qt.appProcessId parameter stays in the Want that the
234 receiving process gets. Pass the data for that process in other Want
235 parameters.
236
237 \section1 Platform API Examples
238
239 \list
240 \li \l {Qt for HarmonyOS Restart Application Example}
241 \li \l {Qt for HarmonyOS Notification Example}
242 \li \l {Qt for HarmonyOS Window Settings Example}
243 \li \l {Qt for HarmonyOS Native Window ID Example}
244 \li \l {Qt for HarmonyOS Window Drag Key Frame Example}
245 \endlist
246*/