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.
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 The QtHarmony namespace contains free functions for code that runs
38 on the JS thread, such as conversions between QJsonObject and JS
39 objects.
40 \endlist
41
42 Qt's HarmonyOS platform API uses the C Node-API types \c napi_env and
43 \c napi_value. They are part of the HarmonyOS SDK and have a stable ABI.
44 Node-API is a low-level, weakly typed C API that requires manual error
45 handling and resource management. It is strongly recommended that
46 applications use
47 \l {https://github.com/nodejs/node-addon-api}{node-addon-api}, the
48 header-only C++ wrapper for Node-API that the Node.js project publishes.
49 Its types convert to and from the C types at no cost. The snippet in
50 the next section uses it.
51
52 \section1 The Two Threads
53
54 The Qt thread is the thread of the QCoreApplication object, which runs
55 the Qt event loop. Platform APIs are available only on the JS thread. To
56 run code there, pass it as a task to
57 QNativeInterface::QHarmonyApplication::runOnJsThread():
58
59 \code
60 const QJsonObject vibrationEffect{
61 {"type", "time"},
62 {"duration", 200},
63 };
64 const QJsonObject vibrationAttribute{
65 {"usage", "touch"},
66 };
67
68 // ...
69
70 QNativeInterface::QHarmonyApplication::runOnJsThread(
71 [vibrationEffect, vibrationAttribute](QHarmonyJsContext &jsContext) {
72 Napi::Env env(jsContext.env());
73 Napi::Object vibratorModule(env, jsContext.tryImportModule("@ohos.vibrator"));
74 if (vibratorModule.IsEmpty())
75 return;
76
77 Napi::Object jsVibrationEffect(env, QtHarmony::mapQJsonObjectToJsObject(env, vibrationEffect));
78 Napi::Object jsVibrationAttribute(env, QtHarmony::mapQJsonObjectToJsObject(env, vibrationAttribute));
79
80 auto startVibrationFunc = vibratorModule.Get("startVibration").As<Napi::Function>();
81 auto startVibrationPromise = startVibrationFunc.Call(vibratorModule, {jsVibrationEffect, jsVibrationAttribute}).As<Napi::Object>();
82 startVibrationPromise.Get("then").As<Napi::Function>().Call(
83 startVibrationPromise,
84 {
85 Napi::Function::New(
86 env,
87 [](const Napi::CallbackInfo &) {
88 qDebug() << "startVibration() succeeded";
89 }),
90 Napi::Function::New(
91 env,
92 [](const Napi::CallbackInfo &info) {
93 qWarning() << "startVibration() failed:" << QString::fromStdString(info[0].ToString());
94 }),
95 });
96 });
97 \endcode
98
99 Code that runs on the JS thread must follow the rules listed in the
100 QNativeInterface::QHarmonyApplication documentation. To send results
101 back to the Qt thread, use
102 QNativeInterface::QHarmonyApplication::runOnQtThread(). To pass JS data
103 to the Qt thread, convert it to a QJsonObject with
104 QtHarmony::mapJsObjectToQJsonObject(), or keep it in a
105 QHarmonyJsObjectRef.
106
107 \section1 JS Callbacks
108
109 Asynchronous platform APIs return promises or take callbacks. To handle
110 a promise or to pass a callback, create a function with
111 \c napi_create_function(). Such a function receives the \c napi_env, but
112 not a QHarmonyJsContext. To get the context, call
113 QHarmonyJsContext::fromEnv(). These functions run on the JS thread and
114 follow the same rules as tasks passed to
115 QNativeInterface::QHarmonyApplication::runOnJsThread().
116
117 \section1 JS Modules
118
119 QHarmonyJsContext::tryImportModule() returns a JS module by name, for
120 example \c {@ohos.app.ability.AbilityConstant}. It loads the module on
121 first use. Enumerations are properties of the module object or of a
122 namespace in it. For example, \c LaunchReason is a property of the
123 \c {@ohos.app.ability.AbilityConstant} module object. Enumeration
124 values are properties of their enumeration.
125
126 Native code cannot load kits, the \c {@kit.*} modules. The ArkTS
127 application template bundled with Qt makes the kit modules that Qt
128 needs available to native code. QHarmonyJsContext::tryImportModule()
129 accepts their names, such as \c {@kit.ShareKit.systemShare}.
130
131 \section1 Platform API Examples
132
133 \list
134 \li \l {Qt for HarmonyOS Restart Application Example}
135 \li \l {Qt for HarmonyOS Notification Example}
136 \endlist
137*/