Qt for HarmonyOS/qtohosextras doc/Share Data Receiver Example: Difference between revisions
Fix example page conversion (QTFOROH-2307) |
No edit summary |
||
| (3 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
= Share Data Receiver Example = | = Share Data Receiver Example = | ||
| Line 8: | Line 6: | ||
The window is a log view. At startup the example reads QOhosAppContext::getAppLaunchWantInfo() and calls QOhosWantInfo::tryGetSharedRecordsFromShareKit(), printing the MIME type, content and file path of every record, or a note that no shared data arrived. The same handling is connected to QOhosAbilityContext::newWantInfoReceived() so shares that arrive while the application is already running are logged as well. | The window is a log view. At startup the example reads QOhosAppContext::getAppLaunchWantInfo() and calls QOhosWantInfo::tryGetSharedRecordsFromShareKit(), printing the MIME type, content and file path of every record, or a note that no shared data arrived. The same handling is connected to QOhosAbilityContext::newWantInfoReceived() so shares that arrive while the application is already running are logged as well. | ||
Reading shared records from the launch want: | |||
<syntaxhighlight lang="cpp"> | |||
auto wantInfo = QtOhosExtras::QOhosAppContext::getAppLaunchWantInfo(); | |||
auto sharedDataRecords = wantInfo->tryGetSharedRecordsFromShareKit(); | |||
</syntaxhighlight> | |||
Handling wants that arrive while the application is running: | |||
<syntaxhighlight lang="cpp"> | |||
auto abilityContext = QtOhosExtras::QOhosAbilityContext::instance(); | |||
QObject::connect( | |||
abilityContext, | |||
&QtOhosExtras::QOhosAbilityContext::newWantInfoReceived, | |||
[&](auto wantInfo) { | |||
auto sharedDataRecords = wantInfo->tryGetSharedRecordsFromShareKit(); | |||
window.appendText( | |||
!sharedDataRecords.isNull() | |||
? QStringLiteral("[On New Want] received Shared Data records:\n%1") | |||
.arg(recordsToString(*sharedDataRecords)) | |||
: QStringLiteral("[On New Want] there is no Shared Data.")); | |||
}); | |||
</syntaxhighlight> | |||
'''Note: '''Both the launch want and subsequent wants have to be handled; a null result from tryGetSharedRecordsFromShareKit() means the want carries no shared data. | '''Note: '''Both the launch want and subsequent wants have to be handled; a null result from tryGetSharedRecordsFromShareKit() means the want carries no shared data. | ||
* [[Qt_for_HarmonyOS/qtohosextras_doc/ | Files: | ||
* [[Qt_for_HarmonyOS/qtohosextras_doc/Share_Data_Receiver_Example_-_main.cpp|abilitycontext/sharedatareceiver/main.cpp]] | |||
* [[Qt_for_HarmonyOS/qtohosextras_doc/Share_Data_Receiver_Example_-_sharedatareceiver.pro|abilitycontext/sharedatareceiver/sharedatareceiver.pro]] | |||
[[Category:Qt for HarmonyOS]] | [[Category:Qt for HarmonyOS]] | ||
Latest revision as of 07:54, 17 September 2026
Demonstrates receiving Share Kit records delivered to the application through a want.
The window is a log view. At startup the example reads QOhosAppContext::getAppLaunchWantInfo() and calls QOhosWantInfo::tryGetSharedRecordsFromShareKit(), printing the MIME type, content and file path of every record, or a note that no shared data arrived. The same handling is connected to QOhosAbilityContext::newWantInfoReceived() so shares that arrive while the application is already running are logged as well.
Reading shared records from the launch want:
auto wantInfo = QtOhosExtras::QOhosAppContext::getAppLaunchWantInfo();
auto sharedDataRecords = wantInfo->tryGetSharedRecordsFromShareKit();
Handling wants that arrive while the application is running:
auto abilityContext = QtOhosExtras::QOhosAbilityContext::instance();
QObject::connect(
abilityContext,
&QtOhosExtras::QOhosAbilityContext::newWantInfoReceived,
[&](auto wantInfo) {
auto sharedDataRecords = wantInfo->tryGetSharedRecordsFromShareKit();
window.appendText(
!sharedDataRecords.isNull()
? QStringLiteral("[On New Want] received Shared Data records:\n%1")
.arg(recordsToString(*sharedDataRecords))
: QStringLiteral("[On New Want] there is no Shared Data."));
});
Note: Both the launch want and subsequent wants have to be handled; a null result from tryGetSharedRecordsFromShareKit() means the want carries no shared data.
Files: