Qt for HarmonyOS/qtohosextras doc/Start Ability For Result Example: Difference between revisions
No edit summary |
Publish example source listings and restore Files lists (QTFOROH-2307) |
||
| Line 1: | Line 1: | ||
< [[Qt_for_HarmonyOS/qtohosextras_doc|Qt for HarmonyOS / qtohosextras doc]] | |||
= Start Ability For Result Example = | = Start Ability For Result Example = | ||
| Line 77: | Line 79: | ||
'''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. | ||
* [[Qt_for_HarmonyOS/qtohosextras_doc/ | Files: | ||
* [[Qt_for_HarmonyOS/qtohosextras_doc/Start_Ability_For_Result_Example_-_main.cpp|abilitycontext/startabilityforresult/main.cpp]] | |||
* [[Qt_for_HarmonyOS/qtohosextras_doc/Start_Ability_For_Result_Example_-_startabilityforresult.pro|abilitycontext/startabilityforresult/startabilityforresult.pro]] | |||
[[Category:Qt for HarmonyOS]] | [[Category:Qt for HarmonyOS]] | ||
Latest revision as of 02:54, 17 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.
Files: