Jump to content

Qt for HarmonyOS/qtohosextras doc/Share Data Example

From Qt Wiki
Revision as of 10:18, 16 September 2026 by Shawn Luo (talk | contribs) (Add code snippets to example pages (QTFOROH-2307))

< Qt for HarmonyOS / qtohosextras doc

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().


Images:

  • abilitycontext/sharedata/qt_logo.png