Jump to content

Qt for HarmonyOS/qtohosextras doc/Share Data Example: Difference between revisions

From Qt Wiki
Add code snippets to example pages (QTFOROH-2307)
No edit summary
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
< [[Qt_for_HarmonyOS/qtohosextras_doc|Qt for HarmonyOS / qtohosextras doc]]
= Share Data Example =
= Share Data Example =


Line 76: Line 74:
'''Note: '''shareKitCompleted() can be emitted multiple times for one request, as the user can select several target applications; correlate signals with the request identifier returned by shareDataWithShareKit().
'''Note: '''shareKitCompleted() can be emitted multiple times for one request, as the user can select several target applications; correlate signals with the request identifier returned by shareDataWithShareKit().


Files:
* [[Qt_for_HarmonyOS/qtohosextras_doc/Share_Data_Example_-_main.cpp|abilitycontext/sharedata/main.cpp]]
* [[Qt_for_HarmonyOS/qtohosextras_doc/Share_Data_Example_-_rsc.qrc|abilitycontext/sharedata/rsc.qrc]]
* [[Qt_for_HarmonyOS/qtohosextras_doc/Share_Data_Example_-_sharedata.pro|abilitycontext/sharedata/sharedata.pro]]


Images:
Images:

Latest revision as of 07:54, 17 September 2026

Share Data Example

Demonstrates sharing text, file and URL records with other applications through OHOS Share Kit.


The window builds a list of shared records with ShareKit::createContentRecord(), ShareKit::createFileRecord() and ShareKit::createUrlRecord(), optionally decorated with title, label, description, extra data and a thumbnail. Controller options created with ShareKit::createControllerOptions() allow selecting single-selection mode, the default preview mode and the panel anchor. Share Records calls QOhosAbilityContext::shareDataWithShareKit() on the default instance and logs the returned request identifier; the QOhosAbilityContext::shareKitPanelClosed() and shareKitCompleted() signals are logged as they arrive, the latter delivering the target ability name through ShareKit::QOhosShareOperationResult.

Creating a text record and decorating it:

 QSharedPointer<QtOhosExtras::ShareKit::QOhosSharedRecord> makeTextRecord(int recordIndex)
 {
     auto record = QtOhosExtras::ShareKit::createContentRecord(
         QMimeDatabase().mimeTypeForName("text/plain"),
         QStringLiteral("Record %1 Content").arg(recordIndex));
     setupRecord(record, recordIndex);
     trySetupRecordQtThumbnail(record);

     return record;
 }

Opening the ShareKit panel with the collected records and optional controller options:

 void DemoWindow::shareRecords()
 {
     QSharedPointer<QtOhosExtras::ShareKit::QOhosShareControllerOptions> controllerOptions;
     if (m_useControllerOptionsCheckBox->isChecked()) {
         controllerOptions = QtOhosExtras::ShareKit::createControllerOptions();
         controllerOptions->setSingleSelectionMode(m_singleSelectionModeCheckBox->isChecked());
         controllerOptions->setDefaultPreviewMode(m_defaultPreviewModeCheckBox->isChecked());
         controllerOptions->setAnchor({m_anchorXSpinBox->value(), m_anchorYSpinBox->value()});
     }

     auto abilityContext = QtOhosExtras::QOhosAbilityContext::getDefaultInstance();
     auto requestId = abilityContext->shareDataWithShareKit(m_records, controllerOptions);
     insertOutputLogText("Open ShareKit panel, request id: " + requestId.toHex());
 }

Reacting to the ShareKit signals:

 void DemoWindow::registerOnShareKitEvents()
 {
     auto abilityContext = QtOhosExtras::QOhosAbilityContext::getDefaultInstance();
     QObject::connect(
         abilityContext.get(),
         &QtOhosExtras::QOhosAbilityContext::shareKitPanelClosed,
         this,
         [this](auto requestId) {
             insertOutputLogText("On ShareKit panel closed, request id: " + requestId.toHex());
         });
     QObject::connect(
         abilityContext.get(),
         &QtOhosExtras::QOhosAbilityContext::shareKitCompleted,
         this,
         [this](auto requestId, auto shareOperationResult) {
             insertOutputLogText(
                 "On ShareKit completed, request id: " + requestId.toHex() + " target ability name: "
                 + shareOperationResult->targetAbilityName());
         });
 }

Note: shareKitCompleted() can be emitted multiple times for one request, as the user can select several target applications; correlate signals with the request identifier returned by shareDataWithShareKit().

Files:

Images:

  • abilitycontext/sharedata/qt_logo.png