Qt for HarmonyOS/qtohosextras doc/Share Data Example: Difference between revisions
Add example documentation from qdoc output (QTFOROH-2307) |
No edit summary |
||
| (4 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
= 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: | |||
<syntaxhighlight lang="cpp"> | |||
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; | |||
} | |||
</syntaxhighlight> | |||
Opening the ShareKit panel with the collected records and optional controller options: | |||
<syntaxhighlight lang="cpp"> | |||
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()); | |||
} | |||
</syntaxhighlight> | |||
Reacting to the ShareKit signals: | |||
<syntaxhighlight lang="cpp"> | |||
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()); | |||
}); | |||
} | |||
</syntaxhighlight> | |||
'''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: | |||
* abilitycontext/sharedata/qt_logo.png | |||
[[Category:Qt for HarmonyOS]] | [[Category:Qt for HarmonyOS]] | ||
Latest revision as of 07:54, 17 September 2026
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:
- abilitycontext/sharedata/main.cpp
- abilitycontext/sharedata/rsc.qrc
- abilitycontext/sharedata/sharedata.pro
Images:
- abilitycontext/sharedata/qt_logo.png