QHarmonyJsContext Class
The QHarmonyJsContext class gives code on the JS thread of a HarmonyOS application access to the JS runtime. More...
| Header: | #include <QHarmonyJsContext> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS Core)target_link_libraries(mytarget PRIVATE Qt6::Core) |
| qmake: | QT += core |
| Inherited By: |
Public Functions
| int | env() |
| int | tryImportModule(const std::string &moduleName) |
Static Public Members
| QHarmonyJsContext & | fromEnv(int env) |
Detailed Description
On HarmonyOS, Qt runs the application on its own thread. The ArkTS runtime and the platform UI run on the main thread of the process, called the JS thread. Platform APIs are only available on the JS thread. To run code there, use QNativeInterface::QHarmonyApplication::runOnJsThread(). The code receives a QHarmonyJsContext as the task argument. JS callbacks created with napi_create_function() receive only a napi_env. They get the context with fromEnv().
QHarmonyAppJsContext provides the state of the application in the runtime, such as its default UIAbility instance. To get it, call QHarmonyAppJsContext::fromJsContext().
All member functions must be called on the JS thread. A napi_value that they return is valid until the task or callback that obtained it returns. It must not be passed to another thread. To keep a JS object longer, use a QHarmonyJsObjectRef.
A QHarmonyJsContext reference is valid as long as the napi_env of the context, which is until the task or callback that received or obtained it returns. Get the context again in each task or callback.
See also Qt for HarmonyOS Platform API.
Member Function Documentation
int QHarmonyJsContext::env()
Returns the Node-API environment of the JS thread.
[static] QHarmonyJsContext &QHarmonyJsContext::fromEnv(int env)
Returns the context of env. env must be the environment of the JS thread. The returned context is valid as long as env.
Use this function in a JS callback created with napi_create_function(), which receives only the napi_env.
int QHarmonyJsContext::tryImportModule(const std::string &moduleName)
Returns the JS module moduleName, for example "@ohos.app.ability.AbilityConstant". If the module cannot be loaded, the function logs the error and returns nullptr. No exception is left pending.
The module is loaded on first use and then kept. Further calls with the same name return the same object, as with a JS import. Besides the platform's @ohos.* modules, the kit modules that the ArkTS application template bundled with Qt makes available to native code can be imported, such as @kit.ShareKit.systemShare.
© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.