En esta página

Ejemplo de ID de ventana nativa de Qt para HarmonyOS

Muestra los ID de plataforma de las ventanas de una aplicación Qt en HarmonyOS.

Cada ventana del ejemplo muestra el ID de su ventana de plataforma, el window.Window en el que se muestra la ventana y el ID de la pantalla en la que se encuentra la ventana de plataforma. Las API de la plataforma y otras aplicaciones identifican las ventanas y las pantallas mediante estos ID.

Una ventana obtiene su « window.Window » mediante la función ` QNativeInterface::QHarmonyWindow ` en el hilo de Qt, en forma de identificador ` QHarmonyJsObjectRef `. La función ` getWindowProperties() ` devuelve los ID de forma sincrónica, por lo que el ejemplo los lee con ` QNativeInterface::QHarmonyApplication::runOnJsThreadAndWait()`, que devuelve el resultado de la tarea al hilo de Qt:

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

En el hilo de JS, la tarea desreferencia el identificador y lee los ID a partir de las propiedades de la ventana. El ID de visualización es una propiedad opcional. Cualquier error que lance la llamada se devuelve como texto:

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()),
        };
    }
}

El constructor crea el ` QWindow ` de la ventana y se conecta a ` QWindow::screenChanged()`, de modo que los ID se leen de nuevo cada vez que la ventana se desplaza a otra pantalla:

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

Una ventana lee sus ID justo después de que show() haya devuelto un resultado, cuando su ventana de plataforma existe:

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

Ejecución del ejemplo

Pulsa New sub window y New top-level window. Cada ventana muestra su propio ID de ventana. Una subventana también se muestra en su propia ventana de plataforma.

Mueve una ventana a otra pantalla. El ID de la pantalla cambia con la ventana, mientras que el ID de la ventana permanece igual.

Proyecto de ejemplo en 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.