Jump to content

Qt for HarmonyOS/qtohosextras doc/Preload Example: Difference between revisions

From Qt Wiki
Add example documentation from qdoc output (QTFOROH-2307)
 
Drop manual breadcrumb, MediaWiki renders it automatically (QTFOROH-2307)
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
< [[Qt_for_HarmonyOS/qtohosextras_doc|Qt for HarmonyOS / qtohosextras doc]]
= Preload Example =
= Preload Example =


Line 8: Line 6:


At startup the example inspects QOhosAppContext::getAppLaunchWantInfo() and QOhosWantInfo::launchReason(). When the reason is LaunchReason::StartAbility the window is shown immediately. When the reason is LaunchReason::Preload the window stays hidden; the example connects to QOhosAbilityContext::newWantInfoReceived() and shows the window on the first want whose launch reason is StartAbility, then disconnects the handler.
At startup the example inspects QOhosAppContext::getAppLaunchWantInfo() and QOhosWantInfo::launchReason(). When the reason is LaunchReason::StartAbility the window is shown immediately. When the reason is LaunchReason::Preload the window stays hidden; the example connects to QOhosAbilityContext::newWantInfoReceived() and shows the window on the first want whose launch reason is StartAbility, then disconnects the handler.
The launch reason is read from the launch want info:
<syntaxhighlight lang="cpp">
    const auto wantInfo = QtOhosExtras::QOhosAppContext::getAppLaunchWantInfo();
    const auto appLaunchReason = wantInfo->launchReason();
</syntaxhighlight>
For a preloaded ability, the window is shown on the first StartAbility want:
<syntaxhighlight lang="cpp">
        auto abilityContext = QtOhosExtras::QOhosAbilityContext::getDefaultInstance();
        context->connection = QObject::connect(
            abilityContext.get(),
            &QtOhosExtras::QOhosAbilityContext::newWantInfoReceived,
            context->window,
            [context](auto wantInfo) {
                if (wantInfo->launchReason() == QtOhosExtras::QOhosWantInfo::LaunchReason::StartAbility) {
                    QObject::disconnect(context->connection);
                    context->window->show();
                }
            });
</syntaxhighlight>


Files:
Files:


* [[Qt_for_HarmonyOS/qtohosextras_doc/Preload_Example|abilitycontext/preload/main.cpp]]
* [[Qt_for_HarmonyOS/qtohosextras_doc/Preload_Example_-_main.cpp|abilitycontext/preload/main.cpp]]
* [[Qt_for_HarmonyOS/qtohosextras_doc/Preload_Example|abilitycontext/preload/preload.pro]]
* [[Qt_for_HarmonyOS/qtohosextras_doc/Preload_Example_-_preload.pro|abilitycontext/preload/preload.pro]]


[[Category:Qt for HarmonyOS]]
[[Category:Qt for HarmonyOS]]

Latest revision as of 06:08, 17 September 2026

Preload Example

Demonstrates handling the Preload launch reason so a preloaded ability shows its window only when actually started.


At startup the example inspects QOhosAppContext::getAppLaunchWantInfo() and QOhosWantInfo::launchReason(). When the reason is LaunchReason::StartAbility the window is shown immediately. When the reason is LaunchReason::Preload the window stays hidden; the example connects to QOhosAbilityContext::newWantInfoReceived() and shows the window on the first want whose launch reason is StartAbility, then disconnects the handler.

The launch reason is read from the launch want info:

     const auto wantInfo = QtOhosExtras::QOhosAppContext::getAppLaunchWantInfo();
     const auto appLaunchReason = wantInfo->launchReason();

For a preloaded ability, the window is shown on the first StartAbility want:

         auto abilityContext = QtOhosExtras::QOhosAbilityContext::getDefaultInstance();
         context->connection = QObject::connect(
             abilityContext.get(),
             &QtOhosExtras::QOhosAbilityContext::newWantInfoReceived,
             context->window,
             [context](auto wantInfo) {
                 if (wantInfo->launchReason() == QtOhosExtras::QOhosWantInfo::LaunchReason::StartAbility) {
                     QObject::disconnect(context->connection);
                     context->window->show();
                 }
             });

Files: