Beispiel für den Neustart einer Qt-Anwendung unter HarmonyOS
Startet eine Qt-Anwendung unter HarmonyOS neu und übergibt Daten an die neue Instanz in den Parametern ihres Start-Wants.
Eine Anwendung kann die Plattform auffordern, sie neu zu starten. Der übergebene „Want“ wird zum Start-„Want“ der neuen Instanz und legt somit fest, mit welchen Einstellungen die neu gestartete Anwendung startet. Das Beispiel speichert die Anzahl der Neustarts in einem eigenen „Want“-Parameter, „ org.qtproject.example.restartCount “, und zeigt die Anzahl der laufenden Instanzen an.
Das Beispiel verfügt über ein einziges Fenster, daher hat der Prozess eine einzige „ UIAbility “-Instanz, die Standard-Fähigkeit. Die Eigenschaft „ launchWant “ dieser Fähigkeit ist das „Want“-Objekt, mit dem die Anwendung gestartet wurde. Die erste Schaltfläche übergibt dieses „Want“-Objekt selbst, sodass die neue Instanz mit demselben Zählwert startet. Die zweite Schaltfläche übergibt eine Kopie mit erhöhter Anzahl. Die Kopie wird auf dem Qt-Thread als „ QJsonObject “ erstellt und einem JS-Objekt auf dem JS-Thread zugeordnet:
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);
});
}));Der Neustart selbst ist ein Aufruf von ` restartApp() ` auf dem ` ApplicationContext`, den die Aufgabe über die Standard-Fähigkeit erreicht. Die Aufgabe verwendet `node-addon-api`, den vom Node.js-Projekt veröffentlichten C++-Wrapper für die Node-API, wie wir es für Anwendungen empfehlen:
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()));
}
}Wenn die Plattform die Anfrage akzeptiert, ersetzt die neue Instanz die laufende. Wenn die Plattform die Anfrage ablehnt, beispielsweise weil zu oft Neustarts angefordert wurden, löst ` restartApp() ` synchron einen Fehler aus. `node-addon-api` wandelt den ausgelösten Fehler in eine C++-Ausnahme um, die im Beispiel abgefangen wird. Der Fehler wird dann im Fenster angezeigt, ebenso wie eine fehlende Fähigkeit. Die Aufgabe übermittelt den Text mit ` QNativeInterface::QHarmonyApplication::runOnQtThread()` an den Qt-Thread. Dabei wird ein ` QPointer ` an das Status-Label übergeben, das im Qt-Thread erstellt wurde. Der übermittelte Funktor prüft die ` QPointer `, bevor er das Label aktualisiert:
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));
}Das Beispiel liest beim Start einmal den „Launch Want“-Wert mit ` QNativeInterface::QHarmonyApplication::runOnJsThreadAndWait()` und ` QtHarmony::mapJsObjectToQJsonObject()`. Sowohl die im Fenster angezeigte Zählung als auch der „Want“-Wert der zweiten Schaltfläche stammen daraus.
Ausführen des Beispiels
Drücken Sie die zweite Schaltfläche. Die Anwendung wird neu gestartet und zeigt den um eins erhöhten Zählwert an. Wenn Sie stattdessen die erste Schaltfläche drücken, zeigt die neu gestartete Anwendung denselben Zählwert an.
© 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.