Qt for HarmonyOS Restart Application Example
Restarts a Qt application on HarmonyOS and passes data to the new instance in the parameters of its launch Want.
An application can ask the platform to restart it. The Want that it passes becomes the launch Want of the new instance, so it decides what the restarted application starts with. The example keeps a restart count in a Want parameter of its own, org.qtproject.example.restartCount, and shows the count of the running instance.
The example has a single window, so the process has a single UIAbility instance, the default ability. The launchWant property of that ability is the Want that the application was launched with. The first button passes that Want object itself, so the new instance starts with the same count. The second button passes a copy with the count increased. The copy is built on the Qt thread as a QJsonObject and mapped to a JS object on the JS thread:
layout->addWidget(
createActionButton(
QStringLiteral("Restart with the launch Want\n(the restart count stays the same)"),
&window,
[statusLabel]() {
statusLabel->clear();
QHarmonyApplication::runOnJsThread(
[statusLabel = QPointer<QLabel>(statusLabel)](QHarmonyJsContext &jsContext) {
auto &appJsContext = QHarmonyAppJsContext::fromJsContext(jsContext);
restartApplicationWithWant(appJsContext, appLaunchWant(appJsContext), statusLabel);
});
}));
layout->addWidget(
createActionButton(
QStringLiteral("Restart with the restart count increased"),
&window,
[statusLabel, want = makeWantWithRestartCount(launchWant, restartCount + 1)]() {
statusLabel->clear();
QHarmonyApplication::runOnJsThread(
[want, statusLabel = QPointer<QLabel>(statusLabel)](QHarmonyJsContext &jsContext) {
auto &appJsContext = QHarmonyAppJsContext::fromJsContext(jsContext);
Napi::Object jsWant(appJsContext.env(), QtHarmony::mapQJsonObjectToJsObject(appJsContext.env(), want));
restartApplicationWithWant(appJsContext, jsWant, statusLabel);
});
}));The restart itself is a call to restartApp() on the ApplicationContext, which the task reaches from the default ability. The task uses node-addon-api, the C++ wrapper for Node-API published by the Node.js project, as we recommend for applications:
void restartApplicationWithWant(QHarmonyAppJsContext &appJsContext, Napi::Object want, QPointer<QLabel> statusLabel)
{
Napi::Env env(appJsContext.env());
Napi::Object ability(env, appJsContext.defaultAbility());
if (ability.IsEmpty()) {
showFailureInLabel(statusLabel, QStringLiteral("no ability to restart from"));
return;
}
try {
auto abilityContext = ability.Get("context").As<Napi::Object>();
auto getApplicationContext = abilityContext.Get("getApplicationContext").As<Napi::Function>();
auto applicationContext = getApplicationContext.Call(abilityContext, {}).As<Napi::Object>();
auto restartAppFunc = applicationContext.Get("restartApp").As<Napi::Function>();
restartAppFunc.Call(applicationContext, {want});
} catch (const Napi::Error &error) {
showFailureInLabel(statusLabel, QString::fromUtf8(error.what()));
}
}If the platform accepts the request, the new instance replaces the running one. If the platform refuses, for example because restarts were requested too often, restartApp() throws synchronously. node-addon-api turns the thrown error into a C++ exception, which the example catches. The failure is then shown in the window, as is a missing ability. The task posts the text to the Qt thread with QNativeInterface::QHarmonyApplication::runOnQtThread(). It passes a QPointer to the status label, which was created on the Qt thread. The posted functor checks the QPointer before it updates the label:
void showStatusInLabel(QPointer<QLabel> statusLabel, QString text)
{
QHarmonyApplication::runOnQtThread(
[statusLabel, text]() {
if (statusLabel)
statusLabel->setText(text);
});
}
void showFailureInLabel(QPointer<QLabel> statusLabel, const QString &errorText)
{
showStatusInLabel(statusLabel, QStringLiteral("Failed to restart application: %1").arg(errorText));
}The example reads the launch Want once at startup, with QNativeInterface::QHarmonyApplication::runOnJsThreadAndWait() and QtHarmony::mapJsObjectToQJsonObject(). The count that the window shows and the Want of the second button both come from it.
Running the Example
Press the second button. The application restarts and shows the count increased by one. If you press the first button instead, the restarted application shows the same count.
© 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.