Jump to content

Qt for HarmonyOS/qtohosextras doc/Start Ability For Result Example: Difference between revisions

From Qt Wiki
Add example documentation from qdoc output (QTFOROH-2307)
 
Publish example source listings and restore Files lists (QTFOROH-2307)
 
(3 intermediate revisions by the same user not shown)
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.
Line 13: Line 81:
Files:
Files:


* [[Qt_for_HarmonyOS/qtohosextras_doc/Start_Ability_For_Result_Example|abilitycontext/startabilityforresult/main.cpp]]
* [[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|abilitycontext/startabilityforresult/startabilityforresult.pro]]
* [[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: