Jump to content

Qt for HarmonyOS/user development guide/how to use QOhosWant

From Qt Wiki
Revision as of 03:46, 22 May 2026 by Shawn Luo (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

English 中文

Overview

QOhosWant is the Qt wrapper for the HarmonyOS Want object. Its fields map one-to-one with the native Want:

  • deviceId
  • bundleName
  • moduleName
  • abilityName
  • uri
  • type
  • action
  • entities
  • flags
  • parameters
  • fds

The parameters field is the complete Want parameter set as a QJsonObject, and is not modified by the framework.

Obtaining a Want

On application start (OnCreate)

auto wantInfo = QtOhosExtras::QOhosAppContext::getAppLaunchWantInfo();

Receiving a new Want at runtime (onNewWant)

Listen to the signal

QtOhosExtras::QOhosAbilityContext::newWantInfoReceived(QSharedPointer<QOhosWantInfo>)

.

Call want() on the QOhosWantInfo instance to obtain a QOhosWant.

Reading parameters

Use the standard Qt JSON API to access QOhosWant::parameters (a QJsonObject):

auto params = want.parameters;

Type checks

  • isString()
  • isDouble()
  • isBool()
  • isArray()
  • isObject()

Handling string-or-array values

If a field may be an array, check with isArray() first and then iterate via toArray(). To support both string and array forms transparently:

auto toList = [](const QJsonValue &v) {
    if (v.isString()) return QStringList{v.toString()};
    if (v.isArray()) {
        QStringList out;
        for (const auto &item : v.toArray())
            if (item.isString()) out.append(item.toString());
        return out;
    }
    return QStringList{};
};
auto streams = toList(params.value("ability.params.stream"));

Debug printing

QString json = QJsonDocument(params).toJson(QJsonDocument::Compact);

Complete example (minimal log window)

#include <QApplication>
#include <QJsonDocument>
#include <QScrollBar>
#include <QTextEdit>
#include <QtOhosExtras>

static QStringList tryExtractStreamParam(const QJsonObject &parameters) {
    auto toList = [](const QJsonValue &v) {
        if (v.isString()) return QStringList{v.toString()};
        if (v.isArray()) {
            QStringList out;
            for (const auto &item : v.toArray())
                if (item.isString()) out.append(item.toString());
            return out;
        }
        return QStringList{};
    };
    // Flat (non-nested) key
    auto flat = toList(parameters.value("ability.params.stream"));
    if (!flat.isEmpty())
        return flat;
    // Nested key
    return toList(parameters.value("ability").toObject()
                           .value("params").toObject()
                           .value("stream"));
}

int main(int argc, char **argv) {
    QApplication app(argc, argv);
    QTextEdit view;
    view.setReadOnly(true);
    view.setWindowTitle("Want Parameters Demo");

    auto logWant = [&](const char *tag, const QtOhosExtras::QOhosWant &want) {
        view.append(QStringLiteral("[%1] parameters: %2").arg(tag,
            QString::fromUtf8(QJsonDocument(want.parameters).toJson(QJsonDocument::Compact))));
        auto streams = tryExtractStreamParam(want.parameters);
        view.append(streams.isEmpty()
            ? QStringLiteral("[%1] ability.params.stream not found").arg(tag)
            : QStringLiteral("[%1] ability.params.stream: %2").arg(tag).arg(streams.join(", ")));
        view.verticalScrollBar()->setValue(view.verticalScrollBar()->maximum());
    };

    if (auto launch = QtOhosExtras::QOhosAppContext::getAppLaunchWantInfo())
        logWant("On Application Start", launch->want());

    auto ctx = QtOhosExtras::QOhosAbilityContext::instance();
    QObject::connect(ctx, &QtOhosExtras::QOhosAbilityContext::newWantInfoReceived,
        [&](auto wantInfo) {
            logWant("On New Want", wantInfo->want()); });

    view.show();
    return app.exec();
}

Common pitfalls

  • parameters.value() returns QJsonValue::Undefined when the key is not present; always check the type before accessing.
  • If the sender uses custom keys, Qt preserves them as-is. If you need to support both flat and nested key forms, handle the disambiguation in your own code.
  • QJsonValue::fromVariant() is used for the default conversion. If the data type does not match, handle the conversion manually.