Auf dieser Seite

Beispiel für native Fenster-IDs unter Qt für HarmonyOS

Zeigt die Plattform-IDs der Fenster einer Qt-Anwendung unter HarmonyOS an.

Jedes Fenster des Beispiels zeigt die ID seines Plattformfensters, den „ window.Window “, in dem das Fenster angezeigt wird, sowie die ID des Displays, auf dem sich das Plattformfenster befindet. Plattform-APIs und andere Anwendungen identifizieren Fenster und Displays anhand dieser IDs.

Ein Fenster erhält seine „ window.Window “ über „ QNativeInterface::QHarmonyWindow “ im Qt-Thread als „ QHarmonyJsObjectRef “-Handle. „ getWindowProperties() “ gibt die IDs synchron zurück, daher liest das Beispiel sie mit „ QNativeInterface::QHarmonyApplication::runOnJsThreadAndWait()“ aus, wodurch das Ergebnis der Aufgabe an den Qt-Thread zurückgegeben wird:

void WindowIdWindow::showWindowIds()
{
    auto *harmonyWindow = windowHandle()->nativeInterface<QHarmonyWindow>();
    const auto jsWindowRef = harmonyWindow ? harmonyWindow->jsWindow() : QHarmonyJsObjectRef();
    const WindowIds windowIds = QHarmonyApplication::runOnJsThreadAndWait(
        [&](QHarmonyJsContext &jsContext) {
            return readWindowIds(jsContext, jsWindowRef);
        });

    if (!windowIds.errorText.isEmpty()) {
        m_idsLabel->setText(QStringLiteral("Reading the IDs failed: %1").arg(windowIds.errorText));
        return;
    }
    m_idsLabel->setText(
        QStringLiteral("Window ID: %1\nDisplay ID: %2")
            .arg(QString::number(windowIds.windowId), formatDisplayId(windowIds.optDisplayId)));
}

Im JS-Thread löst die Aufgabe den Verweis auf das Handle auf und liest die IDs aus den Fenstereigenschaften aus. Die Anzeige-ID ist eine optionale Eigenschaft. Ein vom Aufruf ausgelöster Fehler wird als Text zurückgegeben:

struct WindowIds
{
    std::int64_t windowId = 0;
    std::optional<std::int64_t> optDisplayId;
    QString errorText;
};

WindowIds readWindowIds(QHarmonyJsContext &jsContext, const QHarmonyJsObjectRef &jsWindowRef)
{
    Napi::Env env(jsContext.env());
    Napi::Object jsWindow(env, jsWindowRef.value(jsContext));
    if (jsWindow.IsEmpty()) {
        return WindowIds{
            .errorText = QStringLiteral("the window is not shown in a window.Window"),
        };
    }

    try {
        auto getWindowPropertiesFunc = jsWindow.Get("getWindowProperties").As<Napi::Function>();
        auto windowProperties = getWindowPropertiesFunc.Call(jsWindow, {}).As<Napi::Object>();
        auto displayIdValue = windowProperties.Get("displayId");
        return WindowIds{
            .windowId = windowProperties.Get("id").As<Napi::Number>().Int64Value(),
            .optDisplayId = displayIdValue.IsNumber() ? std::optional(displayIdValue.As<Napi::Number>().Int64Value()) : std::nullopt,
        };
    } catch (const Napi::Error &error) {
        return WindowIds{
            .errorText = QString::fromUtf8(error.what()),
        };
    }
}

Der Konstruktor erstellt die „ QWindow “ des Fensters und stellt eine Verbindung zu „ QWindow::screenChanged()“ her, sodass die IDs jedes Mal erneut gelesen werden, wenn das Fenster auf einen anderen Bildschirm verschoben wird:

create();
connect(windowHandle(), &QWindow::screenChanged, this, &WindowIdWindow::showWindowIds);

Ein Fenster liest seine IDs unmittelbar nach der Rückgabe von `show()`, sofern sein Plattformfenster existiert:

void WindowIdWindow::openWindow(const QString &title, QWidget *parentWindow)
{
    auto *newWindow = new WindowIdWindow(title, parentWindow);
    newWindow->setAttribute(Qt::WA_DeleteOnClose);
    newWindow->show();
    newWindow->showWindowIds();
}

Ausführen des Beispiels

Drücken Sie New sub window und New top-level window. Jedes Fenster zeigt seine eigene Fenster-ID an. Ein Unterfenster wird ebenfalls in einem eigenen Plattformfenster angezeigt.

Verschieben Sie ein Fenster auf einen anderen Bildschirm. Die Bildschirm-ID folgt dem Fenster, während die Fenster-ID gleich bleibt.

Beispielprojekt @ code.qt.io

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.