Jump to content

Qt for HarmonyOS/qtohosextras doc/Share Data Receiver Example - main.cpp

From Qt Wiki

< Qt for HarmonyOS / qtohosextras doc

Share Data Receiver Example - main.cpp

 // Copyright (C) 2025 The Qt Company Ltd.
 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

 #include
 #include
 #include
 #include
 #include
 #include

 namespace {

 QString recordsToString(const QList> &records)
 {
     auto stringOrNA = [](const QString &text) {
         return !text.isNull()
             ? text
             : QStringLiteral("N/A");
     };

     QStringList result;
     for (int i = 0; i < records.size(); ++i) {
         auto record = records[i];
         result.append(QStringLiteral("Record: %1").arg(i));
         result.append(QStringLiteral("• mime type: %1").arg(record->mimeType().name()));
         result.append(QStringLiteral("• content: %1").arg(stringOrNA(record->content())));
         result.append(QStringLiteral("• file path: %1").arg(stringOrNA(record->filePath())));
     }

     return result.join("\n");
 }

 class MessageWindow : public QWidget
 {
 public:
     MessageWindow(QWidget *parent = nullptr);

     void appendText(const QString &text);

 private:
     QTextEdit *m_textEdit;
 };

 MessageWindow::MessageWindow(QWidget *parent)
     : QWidget(parent)
 {
     auto *layout = new QVBoxLayout(this);

     m_textEdit = new QTextEdit(this);
     m_textEdit->setReadOnly(true);
     layout->addWidget(m_textEdit);
 }

 void MessageWindow::appendText(const QString &text)
 {
     m_textEdit->append(text);

     auto *scrollBar = m_textEdit->verticalScrollBar();
     scrollBar->setValue(scrollBar->maximum());
 }

 }

 int main(int argc, char *argv[])
 {
     QApplication app(argc, argv);

     MessageWindow window;
     window.setWindowTitle(QLatin1String("Share Data Receiver"));

     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."));
         });

     auto wantInfo = QtOhosExtras::QOhosAppContext::getAppLaunchWantInfo();
     auto sharedDataRecords = wantInfo->tryGetSharedRecordsFromShareKit();

     window.appendText(
         !sharedDataRecords.isNull()
             ? QStringLiteral("[On Application Start] received Shared Data records:\n%1")
                 .arg(recordsToString(*sharedDataRecords))
             : QStringLiteral("[On Application Start] there is no Shared Data."));

     window.show();

     return app.exec();
 }