Qt for HarmonyOS/qtohosextras doc/Start Ability For Result Example: Difference between revisions
Fix example page conversion (QTFOROH-2307) |
Add code snippets to example pages (QTFOROH-2307) |
||
| Line 8: | Line 8: | ||
Three buttons start a target ability with QOhosAbilityContext::startAbilityForResult(): with a want only, with a want and start options built via QtOhosExtras::createStartOptions(), and with an intentionally empty want to exercise the error path. A dialog collects the target bundle and ability names. Every call returns a request identifier which is logged; results arrive through the startAbilityForResultResponseReceived() signal with the result code and an optional returned want, and failures through startAbilityForResultErrorResponseReceived(). | Three buttons start a target ability with QOhosAbilityContext::startAbilityForResult(): with a want only, with a want and start options built via QtOhosExtras::createStartOptions(), and with an intentionally empty want to exercise the error path. A dialog collects the target bundle and ability names. Every call returns a request identifier which is logged; results arrive through the startAbilityForResultResponseReceived() signal with the result code and an optional returned want, and failures through startAbilityForResultErrorResponseReceived(). | ||
Starting with a want only: | |||
<syntaxhighlight lang="cpp"> | |||
[this, abilityContext]() { | |||
auto want = tryGetWantFromTheUser(); | |||
if (want) { | |||
auto requestId = abilityContext->startAbilityForResult(*want.get()); | |||
appendMessage(requestId, QStringLiteral("sent")); | |||
} | |||
})); | |||
</syntaxhighlight> | |||
Starting with a want and start options: | |||
<syntaxhighlight lang="cpp"> | |||
[this, abilityContext]() { | |||
auto want = tryGetWantFromTheUser(); | |||
if (want) { | |||
auto startOptions = QtOhosExtras::createStartOptions(); | |||
startOptions->setWindowTop(0); | |||
startOptions->setWindowLeft(0); | |||
auto requestId = abilityContext->startAbilityForResult( | |||
*want.get(), *startOptions.get()); | |||
appendMessage(requestId, QStringLiteral("sent")); | |||
} | |||
})); | |||
</syntaxhighlight> | |||
Handling the success and error responses: | |||
<syntaxhighlight lang="cpp"> | |||
auto abilityContext = QtOhosExtras::QOhosAbilityContext::getDefaultInstance(); | |||
QObject::connect( | |||
abilityContext.get(), | |||
&QtOhosExtras::QOhosAbilityContext::startAbilityForResultResponseReceived, | |||
this, | |||
[this](auto requestId, auto resultCode, auto want) { | |||
appendMessage(requestId, QStringLiteral("resultCode: %1").arg(resultCode)); | |||
QMessageBox::information( | |||
nullptr, "Success", | |||
QStringLiteral("The result code from started ability: %1, has want: %2, request id: %3") | |||
.arg(resultCode) | |||
.arg(want != nullptr) | |||
.arg(requestId.toHex().constData())); | |||
}); | |||
QObject::connect( | |||
abilityContext.get(), | |||
&QtOhosExtras::QOhosAbilityContext::startAbilityForResultErrorResponseReceived, | |||
this, | |||
[this](auto requestId) { | |||
appendMessage(requestId, QStringLiteral("Error")); | |||
QMessageBox::warning( | |||
nullptr, "Error", | |||
QStringLiteral("An error occurred while starting ability for result. Request id: %1") | |||
.arg(requestId.toHex().constData()) | |||
); | |||
}); | |||
</syntaxhighlight> | |||
'''Note: '''Requests are asynchronous and several can be outstanding, so results must be correlated with the returned request identifier; there is no blocking variant of startAbilityForResult(). At minimum the bundle name and the ability name must be set on the want. | '''Note: '''Requests are asynchronous and several can be outstanding, so results must be correlated with the returned request identifier; there is no blocking variant of startAbilityForResult(). At minimum the bundle name and the ability name must be set on the want. | ||
Revision as of 10:19, 16 September 2026
< Qt for HarmonyOS / qtohosextras doc
Start Ability For Result Example
Demonstrates starting another UIAbility and receiving its result asynchronously.
Three buttons start a target ability with QOhosAbilityContext::startAbilityForResult(): with a want only, with a want and start options built via QtOhosExtras::createStartOptions(), and with an intentionally empty want to exercise the error path. A dialog collects the target bundle and ability names. Every call returns a request identifier which is logged; results arrive through the startAbilityForResultResponseReceived() signal with the result code and an optional returned want, and failures through startAbilityForResultErrorResponseReceived().
Starting with a want only:
[this, abilityContext]() {
auto want = tryGetWantFromTheUser();
if (want) {
auto requestId = abilityContext->startAbilityForResult(*want.get());
appendMessage(requestId, QStringLiteral("sent"));
}
}));
Starting with a want and start options:
[this, abilityContext]() {
auto want = tryGetWantFromTheUser();
if (want) {
auto startOptions = QtOhosExtras::createStartOptions();
startOptions->setWindowTop(0);
startOptions->setWindowLeft(0);
auto requestId = abilityContext->startAbilityForResult(
*want.get(), *startOptions.get());
appendMessage(requestId, QStringLiteral("sent"));
}
}));
Handling the success and error responses:
auto abilityContext = QtOhosExtras::QOhosAbilityContext::getDefaultInstance();
QObject::connect(
abilityContext.get(),
&QtOhosExtras::QOhosAbilityContext::startAbilityForResultResponseReceived,
this,
[this](auto requestId, auto resultCode, auto want) {
appendMessage(requestId, QStringLiteral("resultCode: %1").arg(resultCode));
QMessageBox::information(
nullptr, "Success",
QStringLiteral("The result code from started ability: %1, has want: %2, request id: %3")
.arg(resultCode)
.arg(want != nullptr)
.arg(requestId.toHex().constData()));
});
QObject::connect(
abilityContext.get(),
&QtOhosExtras::QOhosAbilityContext::startAbilityForResultErrorResponseReceived,
this,
[this](auto requestId) {
appendMessage(requestId, QStringLiteral("Error"));
QMessageBox::warning(
nullptr, "Error",
QStringLiteral("An error occurred while starting ability for result. Request id: %1")
.arg(requestId.toHex().constData())
);
});
Note: Requests are asynchronous and several can be outstanding, so results must be correlated with the returned request identifier; there is no blocking variant of startAbilityForResult(). At minimum the bundle name and the ability name must be set on the want.